From 08f560f388c2eceb2e198cc0813664917f88aca2 Mon Sep 17 00:00:00 2001 From: imbajin Date: Sat, 6 May 2023 19:21:16 +0800 Subject: [PATCH] refact: unify LF line separator Change-Id: I3f38685af534468a51b79b7f45d24fdb30a74f34 --- .../apache/hugegraph/pd/meta/QueueStore.java | 68 +-- .../hugegraph/pd/metrics/PDMetrics.java | 198 ++++---- .../hugegraph/pd/model/PromTargetsModel.java | 144 +++--- .../pd/notice/NoticeBroadcaster.java | 314 ++++++------ .../hugegraph/pd/rest/PromTargetsAPI.java | 144 +++--- .../org/apache/hugegraph/pd/rest/TestAPI.java | 280 +++++------ .../pd/service/PromTargetsService.java | 468 +++++++++--------- .../apache/hugegraph/pd/util/HgMapCache.java | 164 +++--- .../org/apache/hugegraph/pd/util/IdUtil.java | 64 +-- settings.xml | 224 ++++----- 10 files changed, 1034 insertions(+), 1034 deletions(-) diff --git a/hg-pd-core/src/main/java/org/apache/hugegraph/pd/meta/QueueStore.java b/hg-pd-core/src/main/java/org/apache/hugegraph/pd/meta/QueueStore.java index b3eb0c971..6a23615d6 100644 --- a/hg-pd-core/src/main/java/org/apache/hugegraph/pd/meta/QueueStore.java +++ b/hg-pd-core/src/main/java/org/apache/hugegraph/pd/meta/QueueStore.java @@ -1,34 +1,34 @@ -package org.apache.hugegraph.pd.meta; - -import com.baidu.hugegraph.pd.common.HgAssert; -import com.baidu.hugegraph.pd.common.PDException; - -import org.apache.hugegraph.pd.config.PDConfig; - -import com.baidu.hugegraph.pd.grpc.Metapb; - -import java.util.List; - -/** - * @author lynn.bond@hotmail.com on 2022/2/10 - */ -public class QueueStore extends MetadataRocksDBStore { - QueueStore(PDConfig pdConfig) { - super(pdConfig); - } - - public void addItem(Metapb.QueueItem queueItem) throws PDException { - HgAssert.isArgumentNotNull(queueItem, "queueItem"); - byte[] key = MetadataKeyHelper.getQueueItemKey(queueItem.getItemId()); - put(key, queueItem.toByteString().toByteArray()); - } - - public void removeItem(String itemId) throws PDException { - remove(MetadataKeyHelper.getQueueItemKey(itemId)); - } - - public List getQueue() throws PDException { - byte[] prefix = MetadataKeyHelper.getQueueItemPrefix(); - return scanPrefix(Metapb.QueueItem.parser(), prefix); - } -} +package org.apache.hugegraph.pd.meta; + +import com.baidu.hugegraph.pd.common.HgAssert; +import com.baidu.hugegraph.pd.common.PDException; + +import org.apache.hugegraph.pd.config.PDConfig; + +import com.baidu.hugegraph.pd.grpc.Metapb; + +import java.util.List; + +/** + * @author lynn.bond@hotmail.com on 2022/2/10 + */ +public class QueueStore extends MetadataRocksDBStore { + QueueStore(PDConfig pdConfig) { + super(pdConfig); + } + + public void addItem(Metapb.QueueItem queueItem) throws PDException { + HgAssert.isArgumentNotNull(queueItem, "queueItem"); + byte[] key = MetadataKeyHelper.getQueueItemKey(queueItem.getItemId()); + put(key, queueItem.toByteString().toByteArray()); + } + + public void removeItem(String itemId) throws PDException { + remove(MetadataKeyHelper.getQueueItemKey(itemId)); + } + + public List getQueue() throws PDException { + byte[] prefix = MetadataKeyHelper.getQueueItemPrefix(); + return scanPrefix(Metapb.QueueItem.parser(), prefix); + } +} diff --git a/hg-pd-service/src/main/java/org/apache/hugegraph/pd/metrics/PDMetrics.java b/hg-pd-service/src/main/java/org/apache/hugegraph/pd/metrics/PDMetrics.java index 17f0dad1d..bb67f78cb 100644 --- a/hg-pd-service/src/main/java/org/apache/hugegraph/pd/metrics/PDMetrics.java +++ b/hg-pd-service/src/main/java/org/apache/hugegraph/pd/metrics/PDMetrics.java @@ -1,99 +1,99 @@ -package org.apache.hugegraph.pd.metrics; - -import com.baidu.hugegraph.pd.common.PDException; -import com.baidu.hugegraph.pd.grpc.Metapb; -import org.apache.hugegraph.pd.service.PDService; -import io.micrometer.core.instrument.Gauge; -import io.micrometer.core.instrument.MeterRegistry; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import java.util.Collections; -import java.util.List; -import java.util.concurrent.atomic.AtomicLong; - -/** - * @author lynn.bond@hotmail.com on 2022/1/5 - */ -@Component -@Slf4j -public final class PDMetrics { - public final static String PREFIX = "hg"; - private final static AtomicLong graphs = new AtomicLong(0); - private MeterRegistry registry; - - @Autowired - private PDService pdService; - - public synchronized void init(MeterRegistry meterRegistry) { - - if (registry == null) { - registry = meterRegistry; - registerMeters(); - } - - } - - private void registerMeters() { - Gauge.builder(PREFIX + ".up", () -> 1).register(registry); - - Gauge.builder(PREFIX + ".graphs", () -> updateGraphs()) - .description("Number of graphs registered in PD") - .register(registry); - - Gauge.builder(PREFIX + ".stores", () -> updateStores()) - .description("Number of stores registered in PD") - .register(registry); - - } - - private long updateGraphs() { - long buf = getGraphs(); - - if (buf != graphs.get()) { - graphs.set(buf); - registerGraphMetrics(); - } - return buf; - } - - private long updateStores() { - return getStores(); - } - - private long getGraphs() { - return getGraphMetas().size(); - } - - private long getStores(){ - try { - return this.pdService.getStoreNodeService().getStores(null).size(); - } catch (PDException e) { - log.error(e.getMessage(),e); - e.printStackTrace(); - } - return 0; - } - - private List getGraphMetas(){ - try { - return this.pdService.getPartitionService().getGraphs(); - } catch (PDException e) { - log.error(e.getMessage(),e); - } - return Collections.EMPTY_LIST; - } - - private void registerGraphMetrics(){ - this.getGraphMetas().forEach(meta->{ - Gauge.builder(PREFIX + ".partitions",this.pdService.getPartitionService() - ,e-> e.getPartitions(meta.getGraphName()).size()) - .description("Number of partitions assigned to a graph") - .tag("graph",meta.getGraphName()) - .register(this.registry); - - }); - } - -} +package org.apache.hugegraph.pd.metrics; + +import com.baidu.hugegraph.pd.common.PDException; +import com.baidu.hugegraph.pd.grpc.Metapb; +import org.apache.hugegraph.pd.service.PDService; +import io.micrometer.core.instrument.Gauge; +import io.micrometer.core.instrument.MeterRegistry; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; + +/** + * @author lynn.bond@hotmail.com on 2022/1/5 + */ +@Component +@Slf4j +public final class PDMetrics { + public final static String PREFIX = "hg"; + private final static AtomicLong graphs = new AtomicLong(0); + private MeterRegistry registry; + + @Autowired + private PDService pdService; + + public synchronized void init(MeterRegistry meterRegistry) { + + if (registry == null) { + registry = meterRegistry; + registerMeters(); + } + + } + + private void registerMeters() { + Gauge.builder(PREFIX + ".up", () -> 1).register(registry); + + Gauge.builder(PREFIX + ".graphs", () -> updateGraphs()) + .description("Number of graphs registered in PD") + .register(registry); + + Gauge.builder(PREFIX + ".stores", () -> updateStores()) + .description("Number of stores registered in PD") + .register(registry); + + } + + private long updateGraphs() { + long buf = getGraphs(); + + if (buf != graphs.get()) { + graphs.set(buf); + registerGraphMetrics(); + } + return buf; + } + + private long updateStores() { + return getStores(); + } + + private long getGraphs() { + return getGraphMetas().size(); + } + + private long getStores(){ + try { + return this.pdService.getStoreNodeService().getStores(null).size(); + } catch (PDException e) { + log.error(e.getMessage(),e); + e.printStackTrace(); + } + return 0; + } + + private List getGraphMetas(){ + try { + return this.pdService.getPartitionService().getGraphs(); + } catch (PDException e) { + log.error(e.getMessage(),e); + } + return Collections.EMPTY_LIST; + } + + private void registerGraphMetrics(){ + this.getGraphMetas().forEach(meta->{ + Gauge.builder(PREFIX + ".partitions",this.pdService.getPartitionService() + ,e-> e.getPartitions(meta.getGraphName()).size()) + .description("Number of partitions assigned to a graph") + .tag("graph",meta.getGraphName()) + .register(this.registry); + + }); + } + +} diff --git a/hg-pd-service/src/main/java/org/apache/hugegraph/pd/model/PromTargetsModel.java b/hg-pd-service/src/main/java/org/apache/hugegraph/pd/model/PromTargetsModel.java index b7deee61b..12203456f 100644 --- a/hg-pd-service/src/main/java/org/apache/hugegraph/pd/model/PromTargetsModel.java +++ b/hg-pd-service/src/main/java/org/apache/hugegraph/pd/model/PromTargetsModel.java @@ -1,72 +1,72 @@ -package org.apache.hugegraph.pd.model; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -/** - * @author lynn.bond@hotmail.com on 2022/2/14 - */ -public class PromTargetsModel { - private static final String LABEL_METRICS_PATH = "__metrics_path__"; - private static final String LABEL_SCHEME = "__scheme__"; - private static final String LABEL_JOB_NAME = "job"; - private static final String LABEL_CLUSTER = "cluster"; - - private Set targets = new HashSet<>(); - private Map labels = new HashMap<>(); - - public static PromTargetsModel of() { - return new PromTargetsModel(); - } - - private PromTargetsModel() {} - - public Set getTargets() { - return targets; - } - - public Map getLabels() { - return labels; - } - - public PromTargetsModel addTarget(String target) { - if (target == null) return this; - this.targets.add(target); - return this; - } - - public PromTargetsModel setTargets(Set targets) { - if (targets != null) { - this.targets = targets; - } - return this; - } - - public PromTargetsModel setMetricsPath(String path) { - return this.addLabel(LABEL_METRICS_PATH, path); - } - - public PromTargetsModel setScheme(String scheme) { - return this.addLabel(LABEL_SCHEME, scheme); - } - - public PromTargetsModel setClusterId(String clusterId){ - return this.addLabel(LABEL_CLUSTER,clusterId); - } - - public PromTargetsModel addLabel(String label, String value) { - if (label == null || value == null) return this; - this.labels.put(label, value); - return this; - } - - @Override - public String toString() { - return "PromTargetModel{" + - "targets=" + targets + - ", labels=" + labels + - '}'; - } -} +package org.apache.hugegraph.pd.model; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * @author lynn.bond@hotmail.com on 2022/2/14 + */ +public class PromTargetsModel { + private static final String LABEL_METRICS_PATH = "__metrics_path__"; + private static final String LABEL_SCHEME = "__scheme__"; + private static final String LABEL_JOB_NAME = "job"; + private static final String LABEL_CLUSTER = "cluster"; + + private Set targets = new HashSet<>(); + private Map labels = new HashMap<>(); + + public static PromTargetsModel of() { + return new PromTargetsModel(); + } + + private PromTargetsModel() {} + + public Set getTargets() { + return targets; + } + + public Map getLabels() { + return labels; + } + + public PromTargetsModel addTarget(String target) { + if (target == null) return this; + this.targets.add(target); + return this; + } + + public PromTargetsModel setTargets(Set targets) { + if (targets != null) { + this.targets = targets; + } + return this; + } + + public PromTargetsModel setMetricsPath(String path) { + return this.addLabel(LABEL_METRICS_PATH, path); + } + + public PromTargetsModel setScheme(String scheme) { + return this.addLabel(LABEL_SCHEME, scheme); + } + + public PromTargetsModel setClusterId(String clusterId){ + return this.addLabel(LABEL_CLUSTER,clusterId); + } + + public PromTargetsModel addLabel(String label, String value) { + if (label == null || value == null) return this; + this.labels.put(label, value); + return this; + } + + @Override + public String toString() { + return "PromTargetModel{" + + "targets=" + targets + + ", labels=" + labels + + '}'; + } +} diff --git a/hg-pd-service/src/main/java/org/apache/hugegraph/pd/notice/NoticeBroadcaster.java b/hg-pd-service/src/main/java/org/apache/hugegraph/pd/notice/NoticeBroadcaster.java index b76897ff9..20f8daab5 100644 --- a/hg-pd-service/src/main/java/org/apache/hugegraph/pd/notice/NoticeBroadcaster.java +++ b/hg-pd-service/src/main/java/org/apache/hugegraph/pd/notice/NoticeBroadcaster.java @@ -1,157 +1,157 @@ -package org.apache.hugegraph.pd.notice; - -import com.baidu.hugegraph.pd.common.HgAssert; -import lombok.extern.slf4j.Slf4j; - -import java.util.function.Function; -import java.util.function.Supplier; - -/** - * @author lynn.bond@hotmail.com on 2022/2/10 - */ -@Slf4j -public class NoticeBroadcaster { - private long noticeId; - private String durableId; - private Supplier noticeSupplier; - private Supplier durableSupplier; - private Function removeFunction; - private int state; //0=ready; 1=notified; 2=done ack; -1=error - private int counter; - private long timestamp; - - public static NoticeBroadcaster of(Supplier noticeSupplier) { - HgAssert.isArgumentNotNull(noticeSupplier, "noticeSupplier"); - return new NoticeBroadcaster(noticeSupplier); - } - - private NoticeBroadcaster(Supplier noticeSupplier) { - this.noticeSupplier = noticeSupplier; - this.timestamp = System.currentTimeMillis(); - } - - public NoticeBroadcaster setDurableSupplier(Supplier durableSupplier) { - this.durableSupplier = durableSupplier; - return this; - } - - public NoticeBroadcaster setRemoveFunction(Function removeFunction) { - this.removeFunction = removeFunction; - return this; - } - - public NoticeBroadcaster notifying() { - - if (this.state >= 2) { - log.warn("Aborted notifying as ack has done. notice: {}", this); - return this; - } - - this.counter++; - - if (this.durableId == null && this.durableSupplier != null) { - try { - this.durableId = this.durableSupplier.get(); - } catch (Throwable t) { - log.error("Failed to invoke durableSupplier, cause by:", t); - } - } - - try { - this.noticeId = this.noticeSupplier.get(); - state = 1; - } catch (Throwable t) { - state = -1; - log.error("Failed to invoke noticeSupplier: {}; cause by: " + this.noticeSupplier.toString(), t); - } - - return this; - } - - public boolean checkAck(long ackNoticeId) { - boolean flag = false; - - if (this.noticeId == ackNoticeId) { - flag = true; - this.state = 2; - } - - if (flag) { - this.doRemoveDurable(); - } - - return flag; - } - - public boolean doRemoveDurable() { - log.info("Removing NoticeBroadcaster is stating, noticeId:{}, durableId: {}" - , this.noticeId, this.durableId); - boolean flag = false; - - if (this.removeFunction == null) { - log.warn("The remove-function hasn't been set."); - return false; - } - - if (this.durableId == null) { - log.warn("The durableId hasn't been set."); - return false; - } - - try { - if (!(flag = this.removeFunction.apply(this.durableId))) { - log.error("Removing NoticeBroadcaster was not complete, noticeId: {}, durableId: {}" - , this.noticeId, this.durableId); - } - } catch (Throwable t) { - log.error("Failed to remove NoticeBroadcaster, noticeId: " - + this.noticeId + ", durableId: " + this.durableId + ". Cause by:", t); - } - - return flag; - } - - public void setDurableId(String durableId) { - - if (HgAssert.isInvalid(durableId)) { - log.warn("Set an invalid durable-id to NoticeBroadcaster."); - } - - this.durableId = durableId; - } - - public long getNoticeId() { - return noticeId; - } - - public int getState() { - return state; - } - - public int getCounter() { - return counter; - } - - public String getDurableId() { - return durableId; - } - - public long getTimestamp() { - return timestamp; - } - - public void setTimestamp(long timestamp) { - this.timestamp = timestamp; - } - - @Override - public String toString() { - return "NoticeBroadcaster{" + - "noticeId=" + noticeId + - ", durableId='" + durableId + '\'' + - ", state=" + state + - ", counter=" + counter + - ", timestamp=" + timestamp + - '}'; - } -} +package org.apache.hugegraph.pd.notice; + +import com.baidu.hugegraph.pd.common.HgAssert; +import lombok.extern.slf4j.Slf4j; + +import java.util.function.Function; +import java.util.function.Supplier; + +/** + * @author lynn.bond@hotmail.com on 2022/2/10 + */ +@Slf4j +public class NoticeBroadcaster { + private long noticeId; + private String durableId; + private Supplier noticeSupplier; + private Supplier durableSupplier; + private Function removeFunction; + private int state; //0=ready; 1=notified; 2=done ack; -1=error + private int counter; + private long timestamp; + + public static NoticeBroadcaster of(Supplier noticeSupplier) { + HgAssert.isArgumentNotNull(noticeSupplier, "noticeSupplier"); + return new NoticeBroadcaster(noticeSupplier); + } + + private NoticeBroadcaster(Supplier noticeSupplier) { + this.noticeSupplier = noticeSupplier; + this.timestamp = System.currentTimeMillis(); + } + + public NoticeBroadcaster setDurableSupplier(Supplier durableSupplier) { + this.durableSupplier = durableSupplier; + return this; + } + + public NoticeBroadcaster setRemoveFunction(Function removeFunction) { + this.removeFunction = removeFunction; + return this; + } + + public NoticeBroadcaster notifying() { + + if (this.state >= 2) { + log.warn("Aborted notifying as ack has done. notice: {}", this); + return this; + } + + this.counter++; + + if (this.durableId == null && this.durableSupplier != null) { + try { + this.durableId = this.durableSupplier.get(); + } catch (Throwable t) { + log.error("Failed to invoke durableSupplier, cause by:", t); + } + } + + try { + this.noticeId = this.noticeSupplier.get(); + state = 1; + } catch (Throwable t) { + state = -1; + log.error("Failed to invoke noticeSupplier: {}; cause by: " + this.noticeSupplier.toString(), t); + } + + return this; + } + + public boolean checkAck(long ackNoticeId) { + boolean flag = false; + + if (this.noticeId == ackNoticeId) { + flag = true; + this.state = 2; + } + + if (flag) { + this.doRemoveDurable(); + } + + return flag; + } + + public boolean doRemoveDurable() { + log.info("Removing NoticeBroadcaster is stating, noticeId:{}, durableId: {}" + , this.noticeId, this.durableId); + boolean flag = false; + + if (this.removeFunction == null) { + log.warn("The remove-function hasn't been set."); + return false; + } + + if (this.durableId == null) { + log.warn("The durableId hasn't been set."); + return false; + } + + try { + if (!(flag = this.removeFunction.apply(this.durableId))) { + log.error("Removing NoticeBroadcaster was not complete, noticeId: {}, durableId: {}" + , this.noticeId, this.durableId); + } + } catch (Throwable t) { + log.error("Failed to remove NoticeBroadcaster, noticeId: " + + this.noticeId + ", durableId: " + this.durableId + ". Cause by:", t); + } + + return flag; + } + + public void setDurableId(String durableId) { + + if (HgAssert.isInvalid(durableId)) { + log.warn("Set an invalid durable-id to NoticeBroadcaster."); + } + + this.durableId = durableId; + } + + public long getNoticeId() { + return noticeId; + } + + public int getState() { + return state; + } + + public int getCounter() { + return counter; + } + + public String getDurableId() { + return durableId; + } + + public long getTimestamp() { + return timestamp; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } + + @Override + public String toString() { + return "NoticeBroadcaster{" + + "noticeId=" + noticeId + + ", durableId='" + durableId + '\'' + + ", state=" + state + + ", counter=" + counter + + ", timestamp=" + timestamp + + '}'; + } +} diff --git a/hg-pd-service/src/main/java/org/apache/hugegraph/pd/rest/PromTargetsAPI.java b/hg-pd-service/src/main/java/org/apache/hugegraph/pd/rest/PromTargetsAPI.java index b7a1ce298..a4c9a0c23 100644 --- a/hg-pd-service/src/main/java/org/apache/hugegraph/pd/rest/PromTargetsAPI.java +++ b/hg-pd-service/src/main/java/org/apache/hugegraph/pd/rest/PromTargetsAPI.java @@ -1,72 +1,72 @@ -package org.apache.hugegraph.pd.rest; - -import org.apache.hugegraph.pd.model.PromTargetsModel; -import org.apache.hugegraph.pd.service.PromTargetsService; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import java.util.Collections; -import java.util.List; -import java.util.Optional; - -/** - * @author lynn.bond@hotmail.com on 2022/2/14 - */ -@RestController -@Slf4j -@RequestMapping("/v1/prom") -public class PromTargetsAPI { - - @Autowired - private PromTargetsService service; - - @GetMapping(value = "/targets/{appName}", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getPromTargets(@PathVariable(value = "appName", required = true) String appName) { - return ResponseEntity.of(Optional.ofNullable(this.service.getTargets(appName))); - } - - @GetMapping(value = "/targets-all", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getPromAllTargets() { - return ResponseEntity.of(Optional.ofNullable(this.service.getAllTargets())); - } - - @GetMapping(value = "/demo/targets/{appName}", produces = MediaType.APPLICATION_JSON_VALUE) - public List getDemoTargets(@PathVariable(value = "appName", required = true) String targetType) { - - PromTargetsModel model =null; - switch (targetType) { - case "node": - model=PromTargetsModel.of() - .addTarget("10.14.139.26:8100") - .addTarget("10.14.139.27:8100") - .addTarget("10.14.139.28:8100") - .setMetricsPath("/metrics") - .setScheme("http"); - break; - case "store": - model=PromTargetsModel.of() - .addTarget("172.20.94.98:8521") - .addTarget("172.20.94.98:8522") - .addTarget("172.20.94.98:8523") - .setMetricsPath("/actuator/prometheus") - .setScheme("http"); - break; - case "pd": - model=PromTargetsModel.of() - .addTarget("172.20.94.98:8620") - .setMetricsPath("/actuator/prometheus"); - - break; - default: - - } - - return Collections.singletonList(model); - } -} +package org.apache.hugegraph.pd.rest; + +import org.apache.hugegraph.pd.model.PromTargetsModel; +import org.apache.hugegraph.pd.service.PromTargetsService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +/** + * @author lynn.bond@hotmail.com on 2022/2/14 + */ +@RestController +@Slf4j +@RequestMapping("/v1/prom") +public class PromTargetsAPI { + + @Autowired + private PromTargetsService service; + + @GetMapping(value = "/targets/{appName}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getPromTargets(@PathVariable(value = "appName", required = true) String appName) { + return ResponseEntity.of(Optional.ofNullable(this.service.getTargets(appName))); + } + + @GetMapping(value = "/targets-all", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getPromAllTargets() { + return ResponseEntity.of(Optional.ofNullable(this.service.getAllTargets())); + } + + @GetMapping(value = "/demo/targets/{appName}", produces = MediaType.APPLICATION_JSON_VALUE) + public List getDemoTargets(@PathVariable(value = "appName", required = true) String targetType) { + + PromTargetsModel model =null; + switch (targetType) { + case "node": + model=PromTargetsModel.of() + .addTarget("10.14.139.26:8100") + .addTarget("10.14.139.27:8100") + .addTarget("10.14.139.28:8100") + .setMetricsPath("/metrics") + .setScheme("http"); + break; + case "store": + model=PromTargetsModel.of() + .addTarget("172.20.94.98:8521") + .addTarget("172.20.94.98:8522") + .addTarget("172.20.94.98:8523") + .setMetricsPath("/actuator/prometheus") + .setScheme("http"); + break; + case "pd": + model=PromTargetsModel.of() + .addTarget("172.20.94.98:8620") + .setMetricsPath("/actuator/prometheus"); + + break; + default: + + } + + return Collections.singletonList(model); + } +} diff --git a/hg-pd-service/src/main/java/org/apache/hugegraph/pd/rest/TestAPI.java b/hg-pd-service/src/main/java/org/apache/hugegraph/pd/rest/TestAPI.java index fe769cd47..e933f6c77 100644 --- a/hg-pd-service/src/main/java/org/apache/hugegraph/pd/rest/TestAPI.java +++ b/hg-pd-service/src/main/java/org/apache/hugegraph/pd/rest/TestAPI.java @@ -1,140 +1,140 @@ -package org.apache.hugegraph.pd.rest; - -import com.baidu.hugegraph.pd.RegistryService; -import com.baidu.hugegraph.pd.common.PDException; -import com.baidu.hugegraph.pd.config.PDConfig; -import com.baidu.hugegraph.pd.grpc.Metapb; -import com.baidu.hugegraph.pd.grpc.discovery.Query; -import com.baidu.hugegraph.pd.grpc.pulse.ChangeShard; -import com.baidu.hugegraph.pd.grpc.pulse.PartitionHeartbeatResponse; -import com.baidu.hugegraph.pd.meta.MetadataFactory; -import com.baidu.hugegraph.pd.meta.QueueStore; - -import org.apache.hugegraph.pd.pulse.PDPulseSubject; -import org.apache.hugegraph.pd.watch.PDWatchSubject; - -import com.google.protobuf.InvalidProtocolBufferException; -import com.google.protobuf.Parser; -import lombok.extern.slf4j.Slf4j; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.*; - -import java.util.HashMap; -import java.util.List; -import java.util.concurrent.atomic.AtomicLong; - -/** - * @author lynn.bond@hotmail.com on 2022/2/9 - */ -@RestController -@Slf4j -@RequestMapping("/test") -public class TestAPI { - - @Autowired - private PDConfig pdConfig; - - @GetMapping(value = "/discovery/{appName}", produces = MediaType.TEXT_PLAIN_VALUE) - @ResponseBody - public String discovery(@PathVariable(value = "appName", required = true)String appName){ - RegistryService register =new RegistryService(pdConfig); - // Query query=Query.newBuilder().setAppName("hugegraph").build(); - AtomicLong label = new AtomicLong(); - HashMap labels = new HashMap<>(); - String labelValue = String.valueOf(label.incrementAndGet()); - //labels.put("address",labelValue); - Query query = Query.newBuilder().build(); - // Query query = Query.newBuilder().setAppName("hugegraph").set.build(); - - return register.getNodes(query).toString(); - } - - @GetMapping(value = "/pulse", produces = MediaType.TEXT_PLAIN_VALUE) - @ResponseBody - public String notifyClient() { - PDPulseSubject.notifyClient( - PartitionHeartbeatResponse.newBuilder() - .setPartition(Metapb.Partition.newBuilder() - .setId(8) - .setGraphName("graphName8")) - - .setChangeShard( - ChangeShard.newBuilder() - .setChangeTypeValue(8) - .addShard(Metapb.Shard.newBuilder() - .setRoleValue(8) - .setStoreId(8) - ) - ) - - ); - return "partition"; - } - - @GetMapping(value = "/partition", produces = MediaType.TEXT_PLAIN_VALUE) - @ResponseBody - public String noticePartition() { - PDWatchSubject.notifyPartitionChange(PDWatchSubject.ChangeType.ALTER, "graph-test", 99); - return "partition"; - } - - @PutMapping(value = "/queue", produces = MediaType.TEXT_PLAIN_VALUE) - @ResponseBody - public String testPutQueue() { - this.putQueue(); - return "queue"; - } - - public void putQueue(){ - PartitionHeartbeatResponse response=PartitionHeartbeatResponse.newBuilder() - .setPartition(Metapb.Partition.newBuilder() - .setId(9) - .setGraphName("graphName")) - .setChangeShard( - ChangeShard.newBuilder() - .setChangeTypeValue(9) - .addShard(Metapb.Shard.newBuilder() - .setRoleValue(9) - .setStoreId(9) - ) - ).build(); - - Metapb.QueueItem.Builder builder=Metapb.QueueItem.newBuilder() - .setItemId("item-id") - .setItemClass("item-class") - .setItemContent(response.toByteString()); - - - QueueStore store= MetadataFactory.newQueueStore(pdConfig); - - try { - store.addItem(builder.setItemId("item-id-1").build()); - store.addItem(builder.setItemId("item-id-2").build()); - store.addItem(builder.setItemId("item-id-3").build()); - } catch (PDException e) { - e.printStackTrace(); - } - List queue=null; - try { - queue=store.getQueue(); - } catch (PDException e) { - e.printStackTrace(); - } - Parser parser= PartitionHeartbeatResponse.parser(); - - queue.stream().forEach(e->{ - PartitionHeartbeatResponse buf=null; - try { - buf=parser.parseFrom(e.getItemContent()); - } catch (InvalidProtocolBufferException ex) { - ex.printStackTrace(); - } - PDPulseSubject.notifyClient( PartitionHeartbeatResponse.newBuilder(buf)); - }); - - - - } -} +package org.apache.hugegraph.pd.rest; + +import com.baidu.hugegraph.pd.RegistryService; +import com.baidu.hugegraph.pd.common.PDException; +import com.baidu.hugegraph.pd.config.PDConfig; +import com.baidu.hugegraph.pd.grpc.Metapb; +import com.baidu.hugegraph.pd.grpc.discovery.Query; +import com.baidu.hugegraph.pd.grpc.pulse.ChangeShard; +import com.baidu.hugegraph.pd.grpc.pulse.PartitionHeartbeatResponse; +import com.baidu.hugegraph.pd.meta.MetadataFactory; +import com.baidu.hugegraph.pd.meta.QueueStore; + +import org.apache.hugegraph.pd.pulse.PDPulseSubject; +import org.apache.hugegraph.pd.watch.PDWatchSubject; + +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.protobuf.Parser; +import lombok.extern.slf4j.Slf4j; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; + +/** + * @author lynn.bond@hotmail.com on 2022/2/9 + */ +@RestController +@Slf4j +@RequestMapping("/test") +public class TestAPI { + + @Autowired + private PDConfig pdConfig; + + @GetMapping(value = "/discovery/{appName}", produces = MediaType.TEXT_PLAIN_VALUE) + @ResponseBody + public String discovery(@PathVariable(value = "appName", required = true)String appName){ + RegistryService register =new RegistryService(pdConfig); + // Query query=Query.newBuilder().setAppName("hugegraph").build(); + AtomicLong label = new AtomicLong(); + HashMap labels = new HashMap<>(); + String labelValue = String.valueOf(label.incrementAndGet()); + //labels.put("address",labelValue); + Query query = Query.newBuilder().build(); + // Query query = Query.newBuilder().setAppName("hugegraph").set.build(); + + return register.getNodes(query).toString(); + } + + @GetMapping(value = "/pulse", produces = MediaType.TEXT_PLAIN_VALUE) + @ResponseBody + public String notifyClient() { + PDPulseSubject.notifyClient( + PartitionHeartbeatResponse.newBuilder() + .setPartition(Metapb.Partition.newBuilder() + .setId(8) + .setGraphName("graphName8")) + + .setChangeShard( + ChangeShard.newBuilder() + .setChangeTypeValue(8) + .addShard(Metapb.Shard.newBuilder() + .setRoleValue(8) + .setStoreId(8) + ) + ) + + ); + return "partition"; + } + + @GetMapping(value = "/partition", produces = MediaType.TEXT_PLAIN_VALUE) + @ResponseBody + public String noticePartition() { + PDWatchSubject.notifyPartitionChange(PDWatchSubject.ChangeType.ALTER, "graph-test", 99); + return "partition"; + } + + @PutMapping(value = "/queue", produces = MediaType.TEXT_PLAIN_VALUE) + @ResponseBody + public String testPutQueue() { + this.putQueue(); + return "queue"; + } + + public void putQueue(){ + PartitionHeartbeatResponse response=PartitionHeartbeatResponse.newBuilder() + .setPartition(Metapb.Partition.newBuilder() + .setId(9) + .setGraphName("graphName")) + .setChangeShard( + ChangeShard.newBuilder() + .setChangeTypeValue(9) + .addShard(Metapb.Shard.newBuilder() + .setRoleValue(9) + .setStoreId(9) + ) + ).build(); + + Metapb.QueueItem.Builder builder=Metapb.QueueItem.newBuilder() + .setItemId("item-id") + .setItemClass("item-class") + .setItemContent(response.toByteString()); + + + QueueStore store= MetadataFactory.newQueueStore(pdConfig); + + try { + store.addItem(builder.setItemId("item-id-1").build()); + store.addItem(builder.setItemId("item-id-2").build()); + store.addItem(builder.setItemId("item-id-3").build()); + } catch (PDException e) { + e.printStackTrace(); + } + List queue=null; + try { + queue=store.getQueue(); + } catch (PDException e) { + e.printStackTrace(); + } + Parser parser= PartitionHeartbeatResponse.parser(); + + queue.stream().forEach(e->{ + PartitionHeartbeatResponse buf=null; + try { + buf=parser.parseFrom(e.getItemContent()); + } catch (InvalidProtocolBufferException ex) { + ex.printStackTrace(); + } + PDPulseSubject.notifyClient( PartitionHeartbeatResponse.newBuilder(buf)); + }); + + + + } +} diff --git a/hg-pd-service/src/main/java/org/apache/hugegraph/pd/service/PromTargetsService.java b/hg-pd-service/src/main/java/org/apache/hugegraph/pd/service/PromTargetsService.java index df641564c..21e8fb28b 100644 --- a/hg-pd-service/src/main/java/org/apache/hugegraph/pd/service/PromTargetsService.java +++ b/hg-pd-service/src/main/java/org/apache/hugegraph/pd/service/PromTargetsService.java @@ -1,234 +1,234 @@ -package org.apache.hugegraph.pd.service; - -import com.baidu.hugegraph.pd.RegistryService; -import com.baidu.hugegraph.pd.common.HgAssert; -import com.baidu.hugegraph.pd.common.PDException; -import com.baidu.hugegraph.pd.config.PDConfig; -import com.baidu.hugegraph.pd.grpc.Metapb; -import com.baidu.hugegraph.pd.grpc.Pdpb; -import com.baidu.hugegraph.pd.grpc.discovery.NodeInfo; -import com.baidu.hugegraph.pd.grpc.discovery.NodeInfos; -import com.baidu.hugegraph.pd.grpc.discovery.Query; - -import org.apache.hugegraph.pd.util.HgMapCache; -import org.apache.hugegraph.pd.model.PromTargetsModel; -import org.apache.hugegraph.pd.rest.MemberAPI; - -import lombok.extern.slf4j.Slf4j; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import java.util.*; -import java.util.function.Supplier; -import java.util.stream.Collectors; - -/** - * @author lynn.bond@hotmail.com on 2022/2/24 - */ -@Service -@Slf4j -public class PromTargetsService { - - @Autowired - private PDConfig pdConfig; - @Autowired - private PDService pdService; - - private RegistryService register; - - private final PromTargetsModel pdModel = PromTargetsModel.of() - .addLabel("__app_name", "pd") - .setScheme("http") - .setMetricsPath("/actuator/prometheus"); - - - private final PromTargetsModel storeModel = PromTargetsModel.of() - .addLabel("__app_name", "store") - .setScheme("http") - .setMetricsPath("/actuator/prometheus"); - - - private HgMapCache> targetsCache = HgMapCache.expiredOf(24 * 60 * 60 * 1000);// expired after 24H. - - private RegistryService getRegister() { - if (this.register == null) { - this.register = new RegistryService(this.pdConfig); - } - return this.register; - } - - public List getAllTargets() { - List res = new LinkedList<>(); - List buf = this.toModels(this.getRegister().getNodes(Query.newBuilder().build())); - - if (buf != null) { - res.addAll(buf); - } - - res.add(getPdTargets()); - res.add(getStoreTargets()); - - return res; - } - - /** - * @param appName - * @return null if it's not existing - */ - public List getTargets(String appName) { - HgAssert.isArgumentNotNull(appName, "appName"); - switch (appName) { - case "pd": - return Collections.singletonList(this.getPdTargets()); - case "store": - return Collections.singletonList(this.getStoreTargets()); - default: - return this.toModels(this.getRegister().getNodes(Query.newBuilder().setAppName(appName).build())); - } - } - - private PromTargetsModel getPdTargets() { - return setTargets(pdModel, () -> this.mergeCache("pd", getPdAddresses())); - } - - private PromTargetsModel getStoreTargets() { - return setTargets(storeModel, () -> this.mergeCache("store", getStoreAddresses())); - } - - private PromTargetsModel setTargets(PromTargetsModel model, Supplier> supplier) { - return model.setTargets(supplier.get()).setClusterId(String.valueOf(pdConfig.getClusterId())); - } - - /* to prevent the failure of connection between pd and store or pd and pd.*/ - //TODO: To add a schedule task to refresh targets, not to retrieve in every time. - private Set mergeCache(String key, Set set) { - Set buf = this.targetsCache.get(key); - - if (buf == null) { - buf = new HashSet<>(); - this.targetsCache.put(key, buf); - } - - if (set != null) { - buf.addAll(set); - } - - return buf; - } - - private List toModels(NodeInfos info) { - if (info == null) { - return null; - } - - List nodes = info.getInfoList(); - if (nodes == null || nodes.isEmpty()) { - return null; - } - - List res = - nodes.stream().map(e -> { - Map labels = e.getLabelsMap(); - - String target = labels.get("target"); - if (HgAssert.isInvalid(target)) return null; - - PromTargetsModel model = PromTargetsModel.of(); - model.addTarget(target); - model.addLabel("__app_name", e.getAppName()); - - labels.forEach((k, v) -> { - k = k.trim(); - switch (k) { - case "metrics": - model.setMetricsPath(v.trim()); - break; - case "scheme": - model.setScheme(v.trim()); - break; - default: - if (k.startsWith("__")) { - model.addLabel(k, v); - } - - } - }); - - - return model; - }) - .filter(e -> e != null) - .collect(Collectors.toList()); - - if (res.isEmpty()) { - return null; - } - return res; - } - - private Set getPdAddresses() { - MemberAPI.CallStreamObserverWrap response = new MemberAPI.CallStreamObserverWrap<>(); - pdService.getMembers(Pdpb.GetMembersRequest.newBuilder().build(), response); - List members = null; - - try { - members = response.get().get(0).getMembersList(); - } catch (Throwable e) { - log.error("Failed to get all pd members.", e); - } - - Set res = new HashSet<>(); - if (members != null) { - members.stream().forEach(e -> res.add(e.getRestUrl())); - } - - return res; - } - - private Set getStoreAddresses() { - Set res = new HashSet<>(); - List stores = null; - try { - stores = pdService.getStoreNodeService().getStores(); - } catch (PDException e) { - log.error("Failed to get all stores.", e); - } - - if (stores != null) { - stores.stream().forEach(e -> { - String buf = this.getRestAddress(e); - if (buf != null) { - res.add(buf); - } - }); - } - - return res; - } - - //TODO: optimized store registry data, to add host:port of REST server. - private String getRestAddress(Metapb.Store store) { - String address = store.getAddress(); - if (address == null || address.isEmpty()) return null; - try { - Optional port = store.getLabelsList().stream().map( - e -> { - if ("rest.port".equals(e.getKey())) { - return e.getValue(); - } - return null; - }).filter(e -> e != null).findFirst(); - - if (port.isPresent()) { - address = address.substring(0, address.indexOf(':') + 1); - address = address + port.get(); - - } - } catch (Throwable t) { - log.error("Failed to extract the REST address of store, cause by:", t); - } - return address; - - } -} +package org.apache.hugegraph.pd.service; + +import com.baidu.hugegraph.pd.RegistryService; +import com.baidu.hugegraph.pd.common.HgAssert; +import com.baidu.hugegraph.pd.common.PDException; +import com.baidu.hugegraph.pd.config.PDConfig; +import com.baidu.hugegraph.pd.grpc.Metapb; +import com.baidu.hugegraph.pd.grpc.Pdpb; +import com.baidu.hugegraph.pd.grpc.discovery.NodeInfo; +import com.baidu.hugegraph.pd.grpc.discovery.NodeInfos; +import com.baidu.hugegraph.pd.grpc.discovery.Query; + +import org.apache.hugegraph.pd.util.HgMapCache; +import org.apache.hugegraph.pd.model.PromTargetsModel; +import org.apache.hugegraph.pd.rest.MemberAPI; + +import lombok.extern.slf4j.Slf4j; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.*; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +/** + * @author lynn.bond@hotmail.com on 2022/2/24 + */ +@Service +@Slf4j +public class PromTargetsService { + + @Autowired + private PDConfig pdConfig; + @Autowired + private PDService pdService; + + private RegistryService register; + + private final PromTargetsModel pdModel = PromTargetsModel.of() + .addLabel("__app_name", "pd") + .setScheme("http") + .setMetricsPath("/actuator/prometheus"); + + + private final PromTargetsModel storeModel = PromTargetsModel.of() + .addLabel("__app_name", "store") + .setScheme("http") + .setMetricsPath("/actuator/prometheus"); + + + private HgMapCache> targetsCache = HgMapCache.expiredOf(24 * 60 * 60 * 1000);// expired after 24H. + + private RegistryService getRegister() { + if (this.register == null) { + this.register = new RegistryService(this.pdConfig); + } + return this.register; + } + + public List getAllTargets() { + List res = new LinkedList<>(); + List buf = this.toModels(this.getRegister().getNodes(Query.newBuilder().build())); + + if (buf != null) { + res.addAll(buf); + } + + res.add(getPdTargets()); + res.add(getStoreTargets()); + + return res; + } + + /** + * @param appName + * @return null if it's not existing + */ + public List getTargets(String appName) { + HgAssert.isArgumentNotNull(appName, "appName"); + switch (appName) { + case "pd": + return Collections.singletonList(this.getPdTargets()); + case "store": + return Collections.singletonList(this.getStoreTargets()); + default: + return this.toModels(this.getRegister().getNodes(Query.newBuilder().setAppName(appName).build())); + } + } + + private PromTargetsModel getPdTargets() { + return setTargets(pdModel, () -> this.mergeCache("pd", getPdAddresses())); + } + + private PromTargetsModel getStoreTargets() { + return setTargets(storeModel, () -> this.mergeCache("store", getStoreAddresses())); + } + + private PromTargetsModel setTargets(PromTargetsModel model, Supplier> supplier) { + return model.setTargets(supplier.get()).setClusterId(String.valueOf(pdConfig.getClusterId())); + } + + /* to prevent the failure of connection between pd and store or pd and pd.*/ + //TODO: To add a schedule task to refresh targets, not to retrieve in every time. + private Set mergeCache(String key, Set set) { + Set buf = this.targetsCache.get(key); + + if (buf == null) { + buf = new HashSet<>(); + this.targetsCache.put(key, buf); + } + + if (set != null) { + buf.addAll(set); + } + + return buf; + } + + private List toModels(NodeInfos info) { + if (info == null) { + return null; + } + + List nodes = info.getInfoList(); + if (nodes == null || nodes.isEmpty()) { + return null; + } + + List res = + nodes.stream().map(e -> { + Map labels = e.getLabelsMap(); + + String target = labels.get("target"); + if (HgAssert.isInvalid(target)) return null; + + PromTargetsModel model = PromTargetsModel.of(); + model.addTarget(target); + model.addLabel("__app_name", e.getAppName()); + + labels.forEach((k, v) -> { + k = k.trim(); + switch (k) { + case "metrics": + model.setMetricsPath(v.trim()); + break; + case "scheme": + model.setScheme(v.trim()); + break; + default: + if (k.startsWith("__")) { + model.addLabel(k, v); + } + + } + }); + + + return model; + }) + .filter(e -> e != null) + .collect(Collectors.toList()); + + if (res.isEmpty()) { + return null; + } + return res; + } + + private Set getPdAddresses() { + MemberAPI.CallStreamObserverWrap response = new MemberAPI.CallStreamObserverWrap<>(); + pdService.getMembers(Pdpb.GetMembersRequest.newBuilder().build(), response); + List members = null; + + try { + members = response.get().get(0).getMembersList(); + } catch (Throwable e) { + log.error("Failed to get all pd members.", e); + } + + Set res = new HashSet<>(); + if (members != null) { + members.stream().forEach(e -> res.add(e.getRestUrl())); + } + + return res; + } + + private Set getStoreAddresses() { + Set res = new HashSet<>(); + List stores = null; + try { + stores = pdService.getStoreNodeService().getStores(); + } catch (PDException e) { + log.error("Failed to get all stores.", e); + } + + if (stores != null) { + stores.stream().forEach(e -> { + String buf = this.getRestAddress(e); + if (buf != null) { + res.add(buf); + } + }); + } + + return res; + } + + //TODO: optimized store registry data, to add host:port of REST server. + private String getRestAddress(Metapb.Store store) { + String address = store.getAddress(); + if (address == null || address.isEmpty()) return null; + try { + Optional port = store.getLabelsList().stream().map( + e -> { + if ("rest.port".equals(e.getKey())) { + return e.getValue(); + } + return null; + }).filter(e -> e != null).findFirst(); + + if (port.isPresent()) { + address = address.substring(0, address.indexOf(':') + 1); + address = address + port.get(); + + } + } catch (Throwable t) { + log.error("Failed to extract the REST address of store, cause by:", t); + } + return address; + + } +} diff --git a/hg-pd-service/src/main/java/org/apache/hugegraph/pd/util/HgMapCache.java b/hg-pd-service/src/main/java/org/apache/hugegraph/pd/util/HgMapCache.java index 7d7b12657..d0cb0e0e2 100644 --- a/hg-pd-service/src/main/java/org/apache/hugegraph/pd/util/HgMapCache.java +++ b/hg-pd-service/src/main/java/org/apache/hugegraph/pd/util/HgMapCache.java @@ -1,82 +1,82 @@ -package org.apache.hugegraph.pd.util; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Supplier; - -/** - * @param - * @param - * @author lynn.bond@hotmail.com on 2022/3/10 - */ -public class HgMapCache { - private Map cache = new ConcurrentHashMap(); - private Supplier expiry; - - public static HgMapCache expiredOf(long interval){ - return new HgMapCache(new CycleIntervalPolicy(interval)); - } - - private HgMapCache(Supplier expiredPolicy) { - this.expiry = expiredPolicy; - } - - private boolean isExpired() { - if (expiry != null && expiry.get()) { - cache.clear(); - return true; - } - return false; - } - - public void put(K key, V value) { - if (key == null || value == null) return; - this.cache.put(key, value); - } - - - public V get(K key) { - if (isExpired()) return null; - return this.cache.get(key); - } - - public void removeAll() { - this.cache.clear(); - } - - public boolean remove(K key) { - if (key != null) { - this.cache.remove(key); - return true; - } - return false; - } - - public Map getAll() { - return this.cache; - } - - private static class CycleIntervalPolicy implements Supplier{ - private long expireTime=0; - private long interval=0; - - public CycleIntervalPolicy(long interval){ - this.interval=interval; - init(); - } - private void init(){ - expireTime=System.currentTimeMillis()+interval; - } - - @Override - public Boolean get() { - if(System.currentTimeMillis()>expireTime){ - init(); - return true; - } - return false; - } - - } - -} +package org.apache.hugegraph.pd.util; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Supplier; + +/** + * @param + * @param + * @author lynn.bond@hotmail.com on 2022/3/10 + */ +public class HgMapCache { + private Map cache = new ConcurrentHashMap(); + private Supplier expiry; + + public static HgMapCache expiredOf(long interval){ + return new HgMapCache(new CycleIntervalPolicy(interval)); + } + + private HgMapCache(Supplier expiredPolicy) { + this.expiry = expiredPolicy; + } + + private boolean isExpired() { + if (expiry != null && expiry.get()) { + cache.clear(); + return true; + } + return false; + } + + public void put(K key, V value) { + if (key == null || value == null) return; + this.cache.put(key, value); + } + + + public V get(K key) { + if (isExpired()) return null; + return this.cache.get(key); + } + + public void removeAll() { + this.cache.clear(); + } + + public boolean remove(K key) { + if (key != null) { + this.cache.remove(key); + return true; + } + return false; + } + + public Map getAll() { + return this.cache; + } + + private static class CycleIntervalPolicy implements Supplier{ + private long expireTime=0; + private long interval=0; + + public CycleIntervalPolicy(long interval){ + this.interval=interval; + init(); + } + private void init(){ + expireTime=System.currentTimeMillis()+interval; + } + + @Override + public Boolean get() { + if(System.currentTimeMillis()>expireTime){ + init(); + return true; + } + return false; + } + + } + +} diff --git a/hg-pd-service/src/main/java/org/apache/hugegraph/pd/util/IdUtil.java b/hg-pd-service/src/main/java/org/apache/hugegraph/pd/util/IdUtil.java index 844f306f8..569702e6c 100644 --- a/hg-pd-service/src/main/java/org/apache/hugegraph/pd/util/IdUtil.java +++ b/hg-pd-service/src/main/java/org/apache/hugegraph/pd/util/IdUtil.java @@ -1,32 +1,32 @@ -package org.apache.hugegraph.pd.util; - -import lombok.extern.slf4j.Slf4j; - -/** - * @author lynn.bond@hotmail.com on 2022/2/8 - */ -@Slf4j -public final class IdUtil { - private final static byte[] lock = new byte[0]; - - public static String createMillisStr(){ - return String.valueOf(createMillisId()); - } - - /** - * Create millisecond style ID; - * @return - */ - public static Long createMillisId() { - synchronized (lock) { - try { - Thread.sleep(1); - } catch (InterruptedException e) { - log.error("Failed to sleep", e); - } - - return System.currentTimeMillis(); - } - - } -} +package org.apache.hugegraph.pd.util; + +import lombok.extern.slf4j.Slf4j; + +/** + * @author lynn.bond@hotmail.com on 2022/2/8 + */ +@Slf4j +public final class IdUtil { + private final static byte[] lock = new byte[0]; + + public static String createMillisStr(){ + return String.valueOf(createMillisId()); + } + + /** + * Create millisecond style ID; + * @return + */ + public static Long createMillisId() { + synchronized (lock) { + try { + Thread.sleep(1); + } catch (InterruptedException e) { + log.error("Failed to sleep", e); + } + + return System.currentTimeMillis(); + } + + } +} diff --git a/settings.xml b/settings.xml index 3158de73c..083a6a0dc 100644 --- a/settings.xml +++ b/settings.xml @@ -1,113 +1,113 @@ - - - - - - star-local - superstar - Superstar12345 - - - star-snapshot - superstar - Superstar12345 - - - - - baidu - - - baidu-nexus - http://maven.baidu-int.com/nexus/content/groups/public - - true - - - false - - - - baidu-nexus-snapshot - http://maven.baidu-int.com/nexus/content/groups/public-snapshots - - false - - - false - - - - - - star - http://10.14.139.8:8082/artifactory/star - - true - always - - - true - always - - - - - - Baidu_Local - http://maven.baidu-int.com/nexus/content/repositories/Baidu_Local - - true - - - false - - - - Baidu_Local_Snapshots - http://maven.baidu-int.com/nexus/content/repositories/Baidu_Local_Snapshots - - false - - - true - always - - - - - - baidu-nexus - http://maven.baidu-int.com/nexus/content/groups/public - - true - - - false - - - - baidu-nexus-snapshot - http://maven.baidu-int.com/nexus/content/groups/public-snapshots - - false - - - true - - - - - - - - - baidu - + + + + + + star-local + superstar + Superstar12345 + + + star-snapshot + superstar + Superstar12345 + + + + + baidu + + + baidu-nexus + http://maven.baidu-int.com/nexus/content/groups/public + + true + + + false + + + + baidu-nexus-snapshot + http://maven.baidu-int.com/nexus/content/groups/public-snapshots + + false + + + false + + + + + + star + http://10.14.139.8:8082/artifactory/star + + true + always + + + true + always + + + + + + Baidu_Local + http://maven.baidu-int.com/nexus/content/repositories/Baidu_Local + + true + + + false + + + + Baidu_Local_Snapshots + http://maven.baidu-int.com/nexus/content/repositories/Baidu_Local_Snapshots + + false + + + true + always + + + + + + baidu-nexus + http://maven.baidu-int.com/nexus/content/groups/public + + true + + + false + + + + baidu-nexus-snapshot + http://maven.baidu-int.com/nexus/content/groups/public-snapshots + + false + + + true + + + + + + + + + baidu + \ No newline at end of file