GlenGeng commented on a change in pull request #1870:
URL: https://github.com/apache/ozone/pull/1870#discussion_r568279824
##########
File path:
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/ScmConfigKeys.java
##########
@@ -195,6 +195,10 @@
public static final String OZONE_SCM_HTTPS_ADDRESS_KEY =
"ozone.scm.https-address";
+ public static final String OZONE_SCM_ADDRESS_KEY =
+ "ozone.om.address";
Review comment:
Should it be `ozone.scm.address` ?
##########
File path:
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/HddsServerUtil.java
##########
@@ -487,4 +501,54 @@ public static MetricsSystem initializeMetrics(
}
return metricsSystem;
}
+
+ /**
+ * Write DB Checkpoint to an output stream as a compressed file (tgz).
+ *
+ * @param checkpoint checkpoint file
+ * @param destination destination output stream.
+ * @throws IOException
+ */
+ public static void writeDBCheckpointToStream(DBCheckpoint checkpoint,
+ OutputStream destination)
+ throws IOException {
+
+ try (CompressorOutputStream gzippedOut = new CompressorStreamFactory()
Review comment:
How about specify multiple resources in a single try-with-resources
statement to avoid the deep indent?
e.g.,
```
try (CompressorOutputStream gzippedOut = new CompressorStreamFactory()
.createCompressorOutputStream(CompressorStreamFactory.GZIP,
destination);
ArchiveOutputStream archiveOutputStream =
new TarArchiveOutputStream(gzippedOut);
Stream<Path> files =
Files.list(checkpoint.getCheckpointLocation())) {
for (Path path : files.collect(Collectors.toList())) {
if (path != null) {
Path fileName = path.getFileName();
if (fileName != null) {
includeFile(path.toFile(), fileName.toString(),
archiveOutputStream);
}
}
}
} catch (CompressorException e) {
throw new IOException(
"Can't compress the checkpoint: " +
checkpoint.getCheckpointLocation(), e);
}
```
##########
File path:
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMGrpcOutputStream.java
##########
@@ -0,0 +1,130 @@
+/*
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.
+ */
+
+/**
+ * Stream to which the tar db checkpoint will be transferred over to the
+ * destination over grpc.
+ * TODO: Make it a generic utility to be used both during container replication
Review comment:
Agree. Too similar to `GrpcOutputStream`.
##########
File path:
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMGrpcClient.java
##########
@@ -0,0 +1,172 @@
+/*
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.hadoop.hdds.scm.ha;
+
+import com.google.common.base.Preconditions;
+import org.apache.hadoop.hdds.conf.ConfigurationSource;
+import org.apache.hadoop.hdds.protocol.scm.proto.InterSCMProtocolProtos;
+import
org.apache.hadoop.hdds.protocol.scm.proto.InterSCMProtocolProtos.CopyDBCheckpointResponseProto;
+import org.apache.hadoop.hdds.protocol.scm.proto.InterSCMProtocolServiceGrpc;
+import org.apache.hadoop.ozone.OzoneConsts;
+import org.apache.ratis.thirdparty.io.grpc.ManagedChannel;
+import org.apache.ratis.thirdparty.io.grpc.netty.NettyChannelBuilder;
+import org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Grpc client to download a Rocks db checkpoint from leader node
+ * in SCM HA ring.
+ */
+public class SCMGrpcClient implements SCMSnapshotDownloader{
+ private static final Logger LOG =
+ LoggerFactory.getLogger(SCMGrpcClient.class);
+
+ private final ManagedChannel channel;
+
+ private final InterSCMProtocolServiceGrpc.InterSCMProtocolServiceStub
+ client;
+
+ private final long timeout;
+
+ public SCMGrpcClient(final String host, final ConfigurationSource conf) {
+ Preconditions.checkNotNull(conf);
+ int port = conf.getObject(SCMHAConfiguration.class).getGrpcBindPort();
+ timeout =
+ conf.getObject(SCMHAConfiguration.class).getGrpcDeadlineInterval();
+ NettyChannelBuilder channelBuilder =
+ NettyChannelBuilder.forAddress(host, port).usePlaintext()
+ .maxInboundMessageSize(OzoneConsts.OZONE_SCM_CHUNK_MAX_SIZE);
+ channel = channelBuilder.build();
+ client = InterSCMProtocolServiceGrpc.newStub(channel).
+ withDeadlineAfter(timeout, TimeUnit.SECONDS);
+ }
+
+
+ @Override
+ public CompletableFuture<Path> download(final Path outputPath) {
+ // By default on every checkpoint, the rocks db will be flushed
+ InterSCMProtocolProtos.CopyDBCheckpointRequestProto request =
+ InterSCMProtocolProtos.CopyDBCheckpointRequestProto.newBuilder()
+ .setFlush(true)
+ .build();
+ CompletableFuture<Path> response = new CompletableFuture<>();
+
+
+ client.download(request,
+ new StreamDownloader(response, outputPath));
+
+ return response;
+ }
+
+ public void shutdown() {
+ channel.shutdown();
+ try {
+ channel.awaitTermination(5, TimeUnit.SECONDS);
+ } catch (Exception e) {
+ LOG.error("failed to shutdown replication channel", e);
+ }
+ }
+
+ @Override
+ public void close() throws Exception {
+ shutdown();
+ }
+
+ /**
+ * gRPC stream observer to CompletableFuture adapter.
+ */
+ public static class StreamDownloader
+ implements StreamObserver<CopyDBCheckpointResponseProto> {
+
+ private final CompletableFuture<Path> response;
+ private final OutputStream stream;
+ private final Path outputPath;
+
+ public StreamDownloader(CompletableFuture<Path> response,
+ Path outputPath) {
+ this.response = response;
+ this.outputPath = outputPath;
+ try {
+ Preconditions.checkNotNull(outputPath, "Output path cannot be null");
+ stream = new FileOutputStream(outputPath.toFile());
+ } catch (IOException e) {
+ throw new UncheckedIOException(
+ "Output path can't be used: " + outputPath, e);
+ }
+ }
+
+ @Override
+ public void onNext(CopyDBCheckpointResponseProto checkPoint) {
+ try {
+ checkPoint.getData().writeTo(stream);
+ } catch (IOException e) {
+ response.completeExceptionally(e);
Review comment:
Shall we do resource cleanup here, just like onError or onComplete ?
##########
File path:
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMGrpcService.java
##########
@@ -0,0 +1,66 @@
+/*
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.hadoop.hdds.scm.ha;
+
+import java.io.IOException;
+
+import com.google.common.base.Preconditions;
+
+import
org.apache.hadoop.hdds.protocol.scm.proto.InterSCMProtocolProtos.CopyDBCheckpointRequestProto;
+import
org.apache.hadoop.hdds.protocol.scm.proto.InterSCMProtocolProtos.CopyDBCheckpointResponseProto;
+import org.apache.hadoop.hdds.protocol.scm.proto.InterSCMProtocolServiceGrpc;
+import org.apache.hadoop.hdds.scm.server.StorageContainerManager;
+import org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Service to handle Rocks db Checkpointing.
+ */
+public class SCMGrpcService extends
Review comment:
Suggestion to the name, there might be more and more grpc service
introduced into SCM, especially it can facilitate security.
```
SCMGrpcProtocolService -> InterSCMGrpcProtocolService
SCMGrpcService -> InterSCMGrpcService
SCMGrpcClient -> InterSCMGrpcClient
```
##########
File path:
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMGrpcProtocolService.java
##########
@@ -0,0 +1,82 @@
+/*
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.hadoop.hdds.scm.ha;
+
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
+
+import com.google.common.base.Preconditions;
+import org.apache.hadoop.hdds.conf.ConfigurationSource;
+import org.apache.hadoop.hdds.scm.server.StorageContainerManager;
+import org.apache.hadoop.ozone.OzoneConsts;
+import org.apache.ratis.thirdparty.io.grpc.Server;
+import org.apache.ratis.thirdparty.io.grpc.ServerBuilder;
+import org.apache.ratis.thirdparty.io.grpc.netty.NettyServerBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Service to serve SCM DB checkpoints available for SCM HA.
+ * Ideally should only be run on a ratis leader.
+ */
+public class SCMGrpcProtocolService {
+ private static final Logger LOG =
+ LoggerFactory.getLogger(SCMGrpcService.class);
+
+ private final int port;
+ private Server server;
+ private boolean isStarted = false;
+
+ public SCMGrpcProtocolService(final ConfigurationSource conf,
+ final StorageContainerManager scm) {
+ Preconditions.checkNotNull(conf);
+ this.port = conf.getObject(SCMHAConfiguration.class).getGrpcBindPort();
+
+ NettyServerBuilder nettyServerBuilder =
+ ((NettyServerBuilder) ServerBuilder.forPort(port))
+ .maxInboundMessageSize(OzoneConsts.OZONE_SCM_CHUNK_MAX_SIZE);
+
+ SCMGrpcService service = new SCMGrpcService(scm);
+ nettyServerBuilder.addService(service);
+ server = nettyServerBuilder.build();
+ }
+
+ public int getPort() {
+ return this.port;
+ }
+
+ public void start() throws IOException {
+ if (!isStarted) {
Review comment:
Not thread safe, how about use atomic variable ?
e.g.,
```
private final AtomicBoolean isStarted = new AtomicBoolean(false);
if (!isStarted.compareAndSet(false, true)) {
LOG.info("Ignore. already started.");
return;
}
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]