You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ratis.apache.org by GitBox <gi...@apache.org> on 2022/12/28 02:10:51 UTC

[GitHub] [ratis] maobaolong commented on a diff in pull request #800: RATIS-1636. Support re-config ratis properties

maobaolong commented on code in PR #800:
URL: https://github.com/apache/ratis/pull/800#discussion_r1058014876


##########
ratis-common/src/main/java/org/apache/ratis/conf/ReconfigurationBase.java:
##########
@@ -0,0 +1,274 @@
+/*
+ * 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.ratis.conf;
+
+import org.apache.ratis.thirdparty.com.google.common.collect.Maps;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+import org.apache.ratis.conf.ReconfigurationUtil.PropertyChange;
+import java.util.Optional;
+
+/**
+ * Utility base class for implementing the Reconfigurable interface.
+ *
+ * Subclasses should override reconfigurePropertyImpl to change individual
+ * properties and getReconfigurableProperties to get all properties that
+ * can be changed at run time.
+ */
+public abstract class ReconfigurationBase implements Reconfigurable {
+
+  private static final Logger LOG = LoggerFactory.getLogger(ReconfigurationBase.class);
+
+  private ReconfigurationUtil reconfigurationUtil = new ReconfigurationUtil();
+  /** Background thread to reload configuration. */
+  private Thread reconfigThread = null;
+  private volatile boolean shouldRun = true;
+  private Object reconfigLock = new Object();
+  private RaftProperties prop;
+  /**
+   * The timestamp when the <code>reconfigThread</code> starts.
+   */
+  private long startTime = 0;
+
+  /**
+   * The timestamp when the <code>reconfigThread</code> finishes.
+   */
+  private long endTime = 0;
+
+
+  /**
+   * A map of <changed property, error message>. If error message is present,
+   * it contains the messages about the error occurred when applies the particular
+   * change. Otherwise, it indicates that the change has been successfully applied.
+   */
+  private Map<PropertyChange, Optional<String>> status = null;
+
+  /**
+   * Construct a ReconfigurableBase.
+   */
+  public ReconfigurationBase() {
+    setConf(new RaftProperties());
+  }
+
+  /**
+   * Construct a ReconfigurableBase with the {@link RaftProperties}
+   * @param properties raft properties.
+   */
+  public ReconfigurationBase(RaftProperties properties) {
+    setConf((properties == null) ? new RaftProperties() : properties);
+  }
+
+  @Override
+  public void setConf(RaftProperties properties) {
+    this.prop = properties;
+  }
+
+  public RaftProperties getConf() {
+    return prop;
+  }
+
+  /**
+   * Create a new raft properties.
+   * @return raftProperties.
+   */
+  protected abstract RaftProperties getNewConf();
+
+  public Collection<PropertyChange> getChangedProperties(
+      RaftProperties newConf, RaftProperties oldConf) {
+    return reconfigurationUtil.parseChangedProperties(newConf, oldConf);
+  }
+
+  /**
+   * A background thread to apply property changes.
+   */
+  private static class ReconfigurationThread extends Thread {

Review Comment:
   You can use call event trigger the property change event and handle all property change in one method.



-- 
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: issues-unsubscribe@ratis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org