|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package io.github.com.artiship.ha; |
| 19 | + |
| 20 | +import io.github.artiship.allo.common.Service; |
| 21 | +import io.github.artiship.allo.model.ha.ZkScheduler; |
| 22 | +import io.github.com.artiship.ha.utils.CuratorUtils; |
| 23 | +import lombok.extern.slf4j.Slf4j; |
| 24 | +import org.apache.curator.framework.CuratorFramework; |
| 25 | +import org.apache.curator.framework.recipes.cache.NodeCache; |
| 26 | +import org.apache.curator.framework.recipes.cache.NodeCacheListener; |
| 27 | + |
| 28 | +import static io.github.com.artiship.ha.GlobalConstants.SCHEDULER_GROUP; |
| 29 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 30 | +import static org.apache.curator.framework.imps.CuratorFrameworkState.STARTED; |
| 31 | + |
| 32 | +@Slf4j |
| 33 | +public class SchedulerLeaderRetrieval implements NodeCacheListener, Service { |
| 34 | + private final CuratorFramework zkClient; |
| 35 | + |
| 36 | + private NodeCache masterCache; |
| 37 | + private ZkScheduler leader; |
| 38 | + |
| 39 | + public SchedulerLeaderRetrieval(CuratorFramework zkClient) { |
| 40 | + this.zkClient = zkClient; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void start() throws Exception { |
| 45 | + try { |
| 46 | + masterCache = |
| 47 | + new NodeCache(zkClient, CuratorUtils.createPath(zkClient, SCHEDULER_GROUP)); |
| 48 | + masterCache.start(true); |
| 49 | + masterCache.getListenable().addListener(this); |
| 50 | + |
| 51 | + retrieve(); |
| 52 | + } catch (Exception e) { |
| 53 | + log.info("Start cache zk master node {} fail", SCHEDULER_GROUP, e); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void nodeChanged() { |
| 59 | + retrieve(); |
| 60 | + } |
| 61 | + |
| 62 | + private void retrieve() { |
| 63 | + leader = ZkScheduler.from(new String(masterCache.getCurrentData().getData(), UTF_8)); |
| 64 | + } |
| 65 | + |
| 66 | + public ZkScheduler getLeader() { |
| 67 | + return this.leader; |
| 68 | + } |
| 69 | + |
| 70 | + public String getMasterHttpUrl() { |
| 71 | + return new StringBuffer("http://") |
| 72 | + .append(leader.getIp()) |
| 73 | + .append(":") |
| 74 | + .append(leader.getHttpPort()) |
| 75 | + .toString(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void stop() throws Exception { |
| 80 | + if (zkClient.getState() == STARTED) { |
| 81 | + zkClient.close(); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments