xiaoyuyao commented on a change in pull request #2216:
URL: https://github.com/apache/ozone/pull/2216#discussion_r641946909



##########
File path: 
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/scm/update/client/CRLClientUpdateHandler.java
##########
@@ -0,0 +1,201 @@
+/*
+ * 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.hadoop.hdds.scm.update.client;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import 
org.apache.hadoop.hdds.protocol.scm.proto.SCMUpdateServiceGrpc.SCMUpdateServiceStub;
+import org.apache.hadoop.hdds.protocol.scm.proto.SCMUpdateServiceProtos;
+import 
org.apache.hadoop.hdds.protocol.scm.proto.SCMUpdateServiceProtos.CRLUpdateRequest;
+import 
org.apache.hadoop.hdds.protocol.scm.proto.SCMUpdateServiceProtos.UpdateRequest;
+import 
org.apache.hadoop.hdds.protocol.scm.proto.SCMUpdateServiceProtos.UpdateResponse;
+import org.apache.hadoop.hdds.scm.update.server.SCMUpdateClientInfo;
+import org.apache.hadoop.hdds.security.x509.crl.CRLInfo;
+import org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * CRL client update handler that handles local CRL update and pending CRLs.
+ */
+public class CRLClientUpdateHandler implements ClientUpdateHandler {
+
+  private static final Logger LOG = LoggerFactory.getLogger(
+      CRLClientUpdateHandler.class);
+  private static final String NAME = "CRLClientUpdateHandler";
+
+  private final SCMUpdateServiceStub updateStub;
+  private final ClientCRLStore clientStore;
+
+  // Used to update server about local pending crl id list
+  private StreamObserver<UpdateRequest> requestObserver;
+  private UUID clientUuid;
+  private SCMUpdateServiceProtos.ClientId clientIdProto;
+
+  // periodically process pending crls
+  private ScheduledExecutorService executorService;
+  private final SCMUpdateServiceGrpcClient serviceGrpcClient;
+  private long crlCheckInterval;
+
+  CRLClientUpdateHandler(UUID clientId,
+      SCMUpdateServiceStub updateStub,
+      SCMUpdateServiceGrpcClient serviceGrpcClient,
+      long crlCheckInterval) {
+    this.clientUuid = clientId;
+    this.updateStub = updateStub;
+    this.serviceGrpcClient = serviceGrpcClient;
+
+    this.clientStore = serviceGrpcClient.getClientCRLStore();
+    this.crlCheckInterval = crlCheckInterval;
+    LOG.info("Pending CRL check interval : {}s", crlCheckInterval/1000);
+    this.executorService = Executors.newSingleThreadScheduledExecutor(
+        new ThreadFactoryBuilder().setDaemon(true)
+            .setNameFormat("CRLUpdateHandler Thread - %d").build());
+  }
+
+  public static Logger getLog() {
+    return LOG;
+  }
+
+  @Override
+  public void handleServerUpdate(UpdateResponse updateResponse) {
+    SCMUpdateServiceProtos.CRLInfoProto crlIfo =
+        updateResponse.getCrlUpdateResponse().getCrlInfo();
+
+    long receivedCrlId = crlIfo.getCrlSequenceID();
+    long localCrlId = clientStore.getLatestCrlId();
+
+    LOG.debug("## Client: clientId {} clientCrlId {} receivedCrlId {}",
+        clientUuid, localCrlId, receivedCrlId);
+    if (localCrlId == receivedCrlId) {
+      return;
+    }
+    // send a client update to refresh stale server
+    if (localCrlId > receivedCrlId) {
+      LOG.warn("Receive stale crlId {} lower than client crlId {}",
+          receivedCrlId, localCrlId);
+      sendClientUpdate();
+      return;
+    }
+
+    CRLInfo crl;
+    try {
+      crl = CRLInfo.fromCRLProto3(crlIfo);
+    } catch (Exception e) {
+      LOG.error("Can't parse server CRL update, skip...", e);
+      return;
+    }
+    clientStore.onRevokeCerts(crl);
+    // send client update.
+    sendClientUpdate();
+  }
+
+  public void start() {
+    // send initial update request to get a request observer handle
+    UpdateRequest updateReq = getUpdateRequest();
+    requestObserver = updateStub.withWaitForReady()
+        .updateStatus(new StreamObserver<UpdateResponse>() {
+          @Override
+          public void onNext(UpdateResponse updateResponse) {
+            LOG.debug("Receive server response: {}", 
updateResponse.toString());
+            serviceGrpcClient.incrUpdateCount();
+            handleServerUpdate(updateResponse);
+          }
+
+          @Override
+          public void onError(Throwable throwable) {
+            LOG.debug("Receive server error ", throwable);
+            serviceGrpcClient.incrErrorCount();
+            if (serviceGrpcClient.getIsRunning().get()) {
+              // TODO: not all server error needs client restart.
+              LOG.warn("Restart client on server error: ", throwable);
+              serviceGrpcClient.restart();
+            }

Review comment:
       The assumption here is to avoid endless loop on client restart, i.e., 
when the client is shutdown(not running), we will not try to restart client to 
handle server errors. 




-- 
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]

Reply via email to