javeme commented on code in PR #2136: URL: https://github.com/apache/incubator-hugegraph/pull/2136#discussion_r1126331475
########## hugegraph-core/src/main/java/org/apache/hugegraph/backend/store/raft/compress/CompressStrategyManager.java: ########## @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package org.apache.hugegraph.backend.store.raft.compress; + +import org.apache.hugegraph.config.CoreOptions; +import org.apache.hugegraph.config.HugeConfig; + +public class CompressStrategyManager { + + private static byte DEFAULT_STRATEGY = 1; + public static final byte SERIAL_STRATEGY = 1; + public static final byte PARALLEL_STRATEGY = 2; + public static final byte MAX_STRATEGY = 5; + private static CompressStrategy[] compressStrategies = new CompressStrategy[MAX_STRATEGY]; + + static { + addCompressStrategy(SERIAL_STRATEGY, new SerialCompressStrategy()); + } + + private CompressStrategyManager() { + } + + public static void addCompressStrategy(final int idx, final CompressStrategy compressStrategy) { Review Comment: can we rename idx to index? ########## hugegraph-core/src/main/java/org/apache/hugegraph/backend/store/raft/compress/CompressStrategyManager.java: ########## @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package org.apache.hugegraph.backend.store.raft.compress; + +import org.apache.hugegraph.config.CoreOptions; +import org.apache.hugegraph.config.HugeConfig; + +public class CompressStrategyManager { + + private static byte DEFAULT_STRATEGY = 1; + public static final byte SERIAL_STRATEGY = 1; + public static final byte PARALLEL_STRATEGY = 2; + public static final byte MAX_STRATEGY = 5; + private static CompressStrategy[] compressStrategies = new CompressStrategy[MAX_STRATEGY]; + + static { + addCompressStrategy(SERIAL_STRATEGY, new SerialCompressStrategy()); + } + + private CompressStrategyManager() { + } + + public static void addCompressStrategy(final int idx, final CompressStrategy compressStrategy) { + if (compressStrategies.length <= idx) { + final CompressStrategy[] newCompressStrategies = new CompressStrategy[idx + 5]; + System.arraycopy(compressStrategies, 0, newCompressStrategies, 0, + compressStrategies.length); + compressStrategies = newCompressStrategies; + } + compressStrategies[idx] = compressStrategy; + } + + public static CompressStrategy getDefault() { + return compressStrategies[DEFAULT_STRATEGY]; + } + + public static void init(final HugeConfig config) { + if (config.get(CoreOptions.RAFT_SNAPSHOT_PARALLEL_COMPRESS)) { + // add parallel compress strategy + if (compressStrategies[PARALLEL_STRATEGY] == null) { + final CompressStrategy compressStrategy = new ParallelCompressStrategy( + config.get(CoreOptions.RAFT_SNAPSHOT_COMPRESS_THREADS), + config.get(CoreOptions.RAFT_SNAPSHOT_DECOMPRESS_THREADS)); + CompressStrategyManager.addCompressStrategy( + CompressStrategyManager.PARALLEL_STRATEGY, compressStrategy); Review Comment: align ########## hugegraph-core/src/main/java/org/apache/hugegraph/backend/store/raft/compress/CompressStrategyManager.java: ########## @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package org.apache.hugegraph.backend.store.raft.compress; + +import org.apache.hugegraph.config.CoreOptions; +import org.apache.hugegraph.config.HugeConfig; + +public class CompressStrategyManager { + + private static byte DEFAULT_STRATEGY = 1; + public static final byte SERIAL_STRATEGY = 1; + public static final byte PARALLEL_STRATEGY = 2; + public static final byte MAX_STRATEGY = 5; + private static CompressStrategy[] compressStrategies = new CompressStrategy[MAX_STRATEGY]; + + static { + addCompressStrategy(SERIAL_STRATEGY, new SerialCompressStrategy()); + } + + private CompressStrategyManager() { + } + + public static void addCompressStrategy(final int idx, final CompressStrategy compressStrategy) { + if (compressStrategies.length <= idx) { + final CompressStrategy[] newCompressStrategies = new CompressStrategy[idx + 5]; + System.arraycopy(compressStrategies, 0, newCompressStrategies, 0, + compressStrategies.length); + compressStrategies = newCompressStrategies; + } + compressStrategies[idx] = compressStrategy; + } + + public static CompressStrategy getDefault() { + return compressStrategies[DEFAULT_STRATEGY]; + } + + public static void init(final HugeConfig config) { + if (config.get(CoreOptions.RAFT_SNAPSHOT_PARALLEL_COMPRESS)) { Review Comment: we prefer to take the early-return style: `return if (!config.get(CoreOptions.RAFT_SNAPSHOT_PARALLEL_COMPRESS))` ########## hugegraph-core/src/main/java/org/apache/hugegraph/backend/store/raft/compress/ParallelCompressStrategy.java: ########## @@ -0,0 +1,252 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package org.apache.hugegraph.backend.store.raft.compress; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Enumeration; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.zip.CheckedInputStream; +import java.util.zip.CheckedOutputStream; +import java.util.zip.Checksum; +import java.util.zip.ZipEntry; + +import org.apache.commons.compress.archivers.zip.ParallelScatterZipCreator; +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; +import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; +import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; +import org.apache.commons.compress.archivers.zip.ZipFile; +import org.apache.commons.compress.parallel.InputStreamSupplier; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.io.input.NullInputStream; +import org.apache.hugegraph.config.CoreOptions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.alipay.sofa.jraft.util.ExecutorServiceHelper; +import com.alipay.sofa.jraft.util.NamedThreadFactory; +import com.alipay.sofa.jraft.util.Requires; +import com.alipay.sofa.jraft.util.ThreadPoolUtil; +import com.google.common.collect.Lists; + +public class ParallelCompressStrategy implements CompressStrategy { + + private static final Logger LOG = LoggerFactory.getLogger(ParallelCompressStrategy.class); + + public static final int QUEUE_SIZE = CoreOptions.CPUS; + public static final long KEEP_ALIVE_SECOND = 300L; + + private final int compressThreads; + private final int decompressThreads; + + public ParallelCompressStrategy(final int compressThreads, final int decompressThreads) { + this.compressThreads = compressThreads; + this.decompressThreads = decompressThreads; + } + + /** + * Parallel output streams controller + */ + private static class ZipArchiveScatterOutputStream { + + private final ParallelScatterZipCreator creator; + + public ZipArchiveScatterOutputStream(final ExecutorService executorService) { + this.creator = new ParallelScatterZipCreator(executorService); + } + + public void addEntry(final ZipArchiveEntry entry, final InputStreamSupplier supplier) { + creator.addArchiveEntry(entry, supplier); + } + + public void writeTo(final ZipArchiveOutputStream archiveOutput) throws Exception { + creator.writeTo(archiveOutput); + } + + } + + @Override + public void compressZip(final String rootDir, final String sourceDir, + final String outputZipFile, + final Checksum checksum) throws Throwable { + final File rootFile = new File(Paths.get(rootDir, sourceDir).toString()); Review Comment: Since there is no "final" mark style in the entire repository, except when necessary. In order to keep a uniform style, can we try to to remove "final" mark for local vars? maybe we can define a specification in the future. ########## hugegraph-core/src/main/java/org/apache/hugegraph/backend/store/raft/compress/CompressStrategyManager.java: ########## @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package org.apache.hugegraph.backend.store.raft.compress; + +import org.apache.hugegraph.config.CoreOptions; +import org.apache.hugegraph.config.HugeConfig; + +public class CompressStrategyManager { + + private static byte DEFAULT_STRATEGY = 1; + public static final byte SERIAL_STRATEGY = 1; + public static final byte PARALLEL_STRATEGY = 2; + public static final byte MAX_STRATEGY = 5; + private static CompressStrategy[] compressStrategies = new CompressStrategy[MAX_STRATEGY]; + + static { + addCompressStrategy(SERIAL_STRATEGY, new SerialCompressStrategy()); + } + + private CompressStrategyManager() { + } + + public static void addCompressStrategy(final int idx, final CompressStrategy compressStrategy) { + if (compressStrategies.length <= idx) { + final CompressStrategy[] newCompressStrategies = new CompressStrategy[idx + 5]; + System.arraycopy(compressStrategies, 0, newCompressStrategies, 0, + compressStrategies.length); + compressStrategies = newCompressStrategies; + } + compressStrategies[idx] = compressStrategy; + } + + public static CompressStrategy getDefault() { + return compressStrategies[DEFAULT_STRATEGY]; + } + + public static void init(final HugeConfig config) { + if (config.get(CoreOptions.RAFT_SNAPSHOT_PARALLEL_COMPRESS)) { + // add parallel compress strategy + if (compressStrategies[PARALLEL_STRATEGY] == null) { + final CompressStrategy compressStrategy = new ParallelCompressStrategy( + config.get(CoreOptions.RAFT_SNAPSHOT_COMPRESS_THREADS), Review Comment: can align with "CompressStrategy" or "compressStrategy" -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@hugegraph.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org