Persist controller leader change history with timestamp for each leader controller.
Project: http://git-wip-us.apache.org/repos/asf/helix/repo Commit: http://git-wip-us.apache.org/repos/asf/helix/commit/998a7bd0 Tree: http://git-wip-us.apache.org/repos/asf/helix/tree/998a7bd0 Diff: http://git-wip-us.apache.org/repos/asf/helix/diff/998a7bd0 Branch: refs/heads/helix-0.6.x Commit: 998a7bd0ce9f7041ec7b47d9415aaf32f59e108f Parents: ac74e1d Author: Lei Xia <l...@linkedin.com> Authored: Wed Oct 12 18:15:10 2016 -0700 Committer: Lei Xia <l...@linkedin.com> Committed: Wed Feb 8 09:56:55 2017 -0800 ---------------------------------------------------------------------- .../org/apache/helix/model/LeaderHistory.java | 11 ++-- .../integration/TestControllerHistory.java | 59 ++++++++++++++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/helix/blob/998a7bd0/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 5b3b5d5..2d66104 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 @@ -35,12 +35,13 @@ import org.apache.helix.ZNRecord; * The history of instances that have served as the leader controller */ public class LeaderHistory extends HelixProperty { - private final static int HISTORY_SIZE = 8; + private final static int HISTORY_SIZE = 10; private enum ConfigProperty { HISTORY, TIME, - DATE + DATE, + CONTROLLER } public LeaderHistory(String id) { @@ -71,7 +72,6 @@ public class LeaderHistory extends HelixProperty { 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>(); @@ -85,12 +85,15 @@ public class LeaderHistory extends HelixProperty { Map<String, String> historyEntry = new HashMap<String, String>(); long currentTime = System.currentTimeMillis(); - DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS"); + DateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss"); df.setTimeZone(TimeZone.getTimeZone("UTC")); String dateTime = df.format(new Date(currentTime)); + historyEntry.put(ConfigProperty.CONTROLLER.name(), instanceName); historyEntry.put(ConfigProperty.TIME.name(), String.valueOf(currentTime)); historyEntry.put(ConfigProperty.DATE.name(), dateTime); + + historyList.add(historyEntry.toString()); } @Override http://git-wip-us.apache.org/repos/asf/helix/blob/998a7bd0/helix-core/src/test/java/org/apache/helix/integration/TestControllerHistory.java ---------------------------------------------------------------------- diff --git a/helix-core/src/test/java/org/apache/helix/integration/TestControllerHistory.java b/helix-core/src/test/java/org/apache/helix/integration/TestControllerHistory.java new file mode 100644 index 0000000..b078c96 --- /dev/null +++ b/helix-core/src/test/java/org/apache/helix/integration/TestControllerHistory.java @@ -0,0 +1,59 @@ +package org.apache.helix.integration; + +/* + * 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. + */ +import org.apache.helix.HelixManager; +import org.apache.helix.HelixManagerFactory; +import org.apache.helix.InstanceType; +import org.apache.helix.PropertyKey; +import org.apache.helix.integration.manager.ClusterControllerManager; +import org.apache.helix.model.LeaderHistory; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.util.List; + +public class TestControllerHistory extends ZkStandAloneCMTestBase { + + @Test() + public void testControllerLeaderHistory() throws Exception { + HelixManager manager = HelixManagerFactory + .getZKHelixManager(CLUSTER_NAME, "admin", InstanceType.ADMINISTRATOR, ZK_ADDR); + manager.connect(); + + PropertyKey.Builder keyBuilder = new PropertyKey.Builder(CLUSTER_NAME); + PropertyKey propertyKey = keyBuilder.controllerLeaderHistory(); + LeaderHistory leaderHistory = manager.getHelixDataAccessor().getProperty(propertyKey); + Assert.assertNotNull(leaderHistory); + List<String> list = leaderHistory.getRecord().getListField("HISTORY"); + Assert.assertEquals(list.size(), 1); + + for (int i = 0; i <= 12; i++) { + _controller.syncStop(); + _controller = new ClusterControllerManager(ZK_ADDR, CLUSTER_NAME, "Controller-" + i); + _controller.syncStart(); + } + + leaderHistory = manager.getHelixDataAccessor().getProperty(propertyKey); + Assert.assertNotNull(leaderHistory); + list = leaderHistory.getRecord().getListField("HISTORY"); + Assert.assertEquals(list.size(), 10); + manager.disconnect(); + } +}