Persist the timestamp along with controller leader change history.

Project: http://git-wip-us.apache.org/repos/asf/helix/repo
Commit: http://git-wip-us.apache.org/repos/asf/helix/commit/ac74e1d3
Tree: http://git-wip-us.apache.org/repos/asf/helix/tree/ac74e1d3
Diff: http://git-wip-us.apache.org/repos/asf/helix/diff/ac74e1d3

Branch: refs/heads/helix-0.6.x
Commit: ac74e1d3010d91ba99c652d204afc8f7a729c4c1
Parents: fc6009f
Author: Lei Xia <l...@linkedin.com>
Authored: Tue Aug 30 13:26:52 2016 -0700
Committer: Lei Xia <l...@linkedin.com>
Committed: Wed Feb 8 09:56:51 2017 -0800

----------------------------------------------------------------------
 .../org/apache/helix/model/LeaderHistory.java   | 36 ++++++++++++++++++++
 1 file changed, 36 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/helix/blob/ac74e1d3/helix-core/src/main/java/org/apache/helix/model/LeaderHistory.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/model/LeaderHistory.java 
b/helix-core/src/main/java/org/apache/helix/model/LeaderHistory.java
index d57fb52..5b3b5d5 100644
--- a/helix-core/src/main/java/org/apache/helix/model/LeaderHistory.java
+++ b/helix-core/src/main/java/org/apache/helix/model/LeaderHistory.java
@@ -19,8 +19,14 @@ package org.apache.helix.model;
  * under the License.
  */
 
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.TimeZone;
 
 import org.apache.helix.HelixProperty;
 import org.apache.helix.ZNRecord;
@@ -31,6 +37,12 @@ import org.apache.helix.ZNRecord;
 public class LeaderHistory extends HelixProperty {
   private final static int HISTORY_SIZE = 8;
 
+  private enum ConfigProperty {
+    HISTORY,
+    TIME,
+    DATE
+  }
+
   public LeaderHistory(String id) {
     super(id);
   }
@@ -45,6 +57,8 @@ public class LeaderHistory extends HelixProperty {
    * @param instanceName the name of the leader instance
    */
   public void updateHistory(String clusterName, String instanceName) {
+    /* keep this for back-compatible */
+    // TODO: remove this in future when we confirmed no one consumes it
     List<String> list = _record.getListField(clusterName);
     if (list == null) {
       list = new ArrayList<String>();
@@ -55,6 +69,28 @@ public class LeaderHistory extends HelixProperty {
       list.remove(0);
     }
     list.add(instanceName);
+    // TODO: remove above in future when we confirmed no one consumes it */
+
+
+    List<String> historyList = 
_record.getListField(ConfigProperty.HISTORY.name());
+    if (historyList == null) {
+      historyList = new ArrayList<String>();
+      _record.setListField(ConfigProperty.HISTORY.name(), historyList);
+    }
+
+    if (historyList.size() == HISTORY_SIZE) {
+      historyList.remove(0);
+    }
+
+    Map<String, String> historyEntry = new HashMap<String, String>();
+
+    long currentTime = System.currentTimeMillis();
+    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS");
+    df.setTimeZone(TimeZone.getTimeZone("UTC"));
+    String dateTime = df.format(new Date(currentTime));
+
+    historyEntry.put(ConfigProperty.TIME.name(), String.valueOf(currentTime));
+    historyEntry.put(ConfigProperty.DATE.name(), dateTime);
   }
 
   @Override

Reply via email to