zyxxoo commented on code in PR #2301: URL: https://github.com/apache/incubator-hugegraph/pull/2301#discussion_r1325528585
########## hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/meta/PdMetaDriver.java: ########## @@ -0,0 +1,250 @@ +/* + * 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.hugegraph.meta; + +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.function.Consumer; + +import org.apache.hugegraph.HugeException; +import org.apache.hugegraph.meta.lock.LockResult; +import org.apache.hugegraph.meta.lock.PdDistributedLock; +import org.apache.hugegraph.pd.client.KvClient; +import org.apache.hugegraph.pd.client.PDClient; +import org.apache.hugegraph.pd.client.PDConfig; +import org.apache.hugegraph.pd.common.PDException; +import org.apache.hugegraph.pd.grpc.kv.KResponse; +import org.apache.hugegraph.pd.grpc.kv.LockResponse; +import org.apache.hugegraph.pd.grpc.kv.ScanPrefixResponse; +import org.apache.hugegraph.pd.grpc.kv.TTLResponse; +import org.apache.hugegraph.pd.grpc.kv.WatchEvent; +import org.apache.hugegraph.pd.grpc.kv.WatchResponse; +import org.apache.hugegraph.pd.grpc.kv.WatchType; + +import com.google.common.base.Strings; + +public class PdMetaDriver implements MetaDriver { + + KvClient<WatchResponse> client = null; + PDClient pdClient = null; + private PdDistributedLock lock; + + public PdMetaDriver(String pdPeer) { + PDConfig pdConfig = PDConfig.of(pdPeer); + this.client = new KvClient<>(pdConfig); + this.pdClient = PDClient.create(pdConfig); + lock = new PdDistributedLock(this.client); + } + + public static void main(String[] args) { + PDConfig pdConfig = PDConfig.of("127.0.0.1:8686"); + KvClient<WatchResponse> client = new KvClient<>(pdConfig); + ScanPrefixResponse contents; + try { + contents = client.scanPrefix("HUGEGRAPH/METRICS"); + Map<String, String> map = contents.getKvsMap(); + for (Map.Entry<String, String> entry : map.entrySet()) { + System.out.println(entry.getKey() + ":" + entry.getValue()); + } + System.out.println(map.size()); + } catch (PDException e) { + e.printStackTrace(); + } + Date dNow = new Date(); + System.out.println(dNow); + SimpleDateFormat ft = new SimpleDateFormat("HHmmss"); + System.out.println("当前时间为: " + ft.format(dNow)); + } + + public PDClient pdClient() { + return this.pdClient; + } + + @Override + public void put(String key, String value) { + try { + this.client.put(key, value); + } catch (PDException e) { + throw new HugeException("Failed to put '%s:%s' to pd", e, key, value); + } + } + + @Override + public String get(String key) { + try { + KResponse response = this.client.get(key); + return response.getValue(); + } catch (PDException e) { + throw new HugeException("Failed to get '%s' from pd", e, key); + } + } + + @Override + public void delete(String key) { + try { + this.client.delete(key); + } catch (PDException e) { + throw new HugeException("Failed to delete '%s' from pd", e, key); + } + } + + @Override + public void deleteWithPrefix(String prefix) { + try { + this.client.deletePrefix(prefix); + } catch (PDException e) { + throw new HugeException("Failed to deleteWithPrefix '%s' from pd", e, prefix); + } + } + + @Override + public Map<String, String> scanWithPrefix(String prefix) { + try { + ScanPrefixResponse response = this.client.scanPrefix(prefix); + return response.getKvsMap(); + } catch (PDException e) { + throw new HugeException("Failed to scanWithPrefix '%s' from pd", e, prefix); + } + } + + @Override + public <T> void listen(String key, Consumer<T> consumer) { + try { + this.client.listen(key, (Consumer<WatchResponse>) consumer); + } catch (PDException e) { + throw new HugeException("Failed to listen '%s' to pd", e, key); + } + } + + @Override + public <T> void listenPrefix(String prefix, Consumer<T> consumer) { + try { + this.client.listenPrefix(prefix, (Consumer<WatchResponse>) consumer); + } catch (PDException e) { + throw new HugeException("Failed to listenPrefix '%s' to pd", e, prefix); + } + } + + @Override + public <T> List<String> extractValuesFromResponse(T response) { + List<String> values = new ArrayList<>(); + WatchResponse res = (WatchResponse) response; + for (WatchEvent event : res.getEventsList()) { + // Skip if not PUT event + if (!event.getType().equals(WatchType.Put)) { + return null; + } + String value = event.getCurrent().getValue(); + values.add(value); + } + return values; + } + + @Override + public <T> Map<String, String> extractKVFromResponse(T response) { + Map<String, String> resultMap = new HashMap<>(); + WatchResponse res = (WatchResponse) response; + for (WatchEvent event : res.getEventsList()) { + // Skip if not etcd PUT event + if (!event.getType().equals(WatchType.Put)) { + continue; + } + + String key = event.getCurrent().getKey(); + String value = event.getCurrent().getValue(); + if (Strings.isNullOrEmpty(key)) { + continue; + } + resultMap.put(key, value); + } + return resultMap; + } + + @Override + public LockResult lock(String key, long ttl) { + while (true) { Review Comment: 如果是 deprecated,就直接删除吧 ########## hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/meta/PdMetaDriver.java: ########## @@ -0,0 +1,250 @@ +/* + * 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.hugegraph.meta; + +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.function.Consumer; + +import org.apache.hugegraph.HugeException; +import org.apache.hugegraph.meta.lock.LockResult; +import org.apache.hugegraph.meta.lock.PdDistributedLock; +import org.apache.hugegraph.pd.client.KvClient; +import org.apache.hugegraph.pd.client.PDClient; +import org.apache.hugegraph.pd.client.PDConfig; +import org.apache.hugegraph.pd.common.PDException; +import org.apache.hugegraph.pd.grpc.kv.KResponse; +import org.apache.hugegraph.pd.grpc.kv.LockResponse; +import org.apache.hugegraph.pd.grpc.kv.ScanPrefixResponse; +import org.apache.hugegraph.pd.grpc.kv.TTLResponse; +import org.apache.hugegraph.pd.grpc.kv.WatchEvent; +import org.apache.hugegraph.pd.grpc.kv.WatchResponse; +import org.apache.hugegraph.pd.grpc.kv.WatchType; + +import com.google.common.base.Strings; + +public class PdMetaDriver implements MetaDriver { + + KvClient<WatchResponse> client = null; + PDClient pdClient = null; + private PdDistributedLock lock; + + public PdMetaDriver(String pdPeer) { + PDConfig pdConfig = PDConfig.of(pdPeer); + this.client = new KvClient<>(pdConfig); + this.pdClient = PDClient.create(pdConfig); + lock = new PdDistributedLock(this.client); + } + + public static void main(String[] args) { + PDConfig pdConfig = PDConfig.of("127.0.0.1:8686"); + KvClient<WatchResponse> client = new KvClient<>(pdConfig); + ScanPrefixResponse contents; + try { + contents = client.scanPrefix("HUGEGRAPH/METRICS"); + Map<String, String> map = contents.getKvsMap(); + for (Map.Entry<String, String> entry : map.entrySet()) { + System.out.println(entry.getKey() + ":" + entry.getValue()); + } + System.out.println(map.size()); + } catch (PDException e) { + e.printStackTrace(); + } + Date dNow = new Date(); + System.out.println(dNow); + SimpleDateFormat ft = new SimpleDateFormat("HHmmss"); + System.out.println("当前时间为: " + ft.format(dNow)); + } + + public PDClient pdClient() { + return this.pdClient; + } + + @Override + public void put(String key, String value) { + try { + this.client.put(key, value); + } catch (PDException e) { + throw new HugeException("Failed to put '%s:%s' to pd", e, key, value); + } + } + + @Override + public String get(String key) { + try { + KResponse response = this.client.get(key); + return response.getValue(); + } catch (PDException e) { + throw new HugeException("Failed to get '%s' from pd", e, key); + } + } + + @Override + public void delete(String key) { + try { + this.client.delete(key); + } catch (PDException e) { + throw new HugeException("Failed to delete '%s' from pd", e, key); + } + } + + @Override + public void deleteWithPrefix(String prefix) { + try { + this.client.deletePrefix(prefix); + } catch (PDException e) { + throw new HugeException("Failed to deleteWithPrefix '%s' from pd", e, prefix); + } + } + + @Override + public Map<String, String> scanWithPrefix(String prefix) { + try { + ScanPrefixResponse response = this.client.scanPrefix(prefix); + return response.getKvsMap(); + } catch (PDException e) { + throw new HugeException("Failed to scanWithPrefix '%s' from pd", e, prefix); + } + } + + @Override + public <T> void listen(String key, Consumer<T> consumer) { + try { + this.client.listen(key, (Consumer<WatchResponse>) consumer); + } catch (PDException e) { + throw new HugeException("Failed to listen '%s' to pd", e, key); + } + } + + @Override + public <T> void listenPrefix(String prefix, Consumer<T> consumer) { + try { + this.client.listenPrefix(prefix, (Consumer<WatchResponse>) consumer); + } catch (PDException e) { + throw new HugeException("Failed to listenPrefix '%s' to pd", e, prefix); + } + } + + @Override + public <T> List<String> extractValuesFromResponse(T response) { + List<String> values = new ArrayList<>(); + WatchResponse res = (WatchResponse) response; + for (WatchEvent event : res.getEventsList()) { + // Skip if not PUT event + if (!event.getType().equals(WatchType.Put)) { + return null; + } + String value = event.getCurrent().getValue(); + values.add(value); + } + return values; + } + + @Override + public <T> Map<String, String> extractKVFromResponse(T response) { + Map<String, String> resultMap = new HashMap<>(); + WatchResponse res = (WatchResponse) response; + for (WatchEvent event : res.getEventsList()) { + // Skip if not etcd PUT event + if (!event.getType().equals(WatchType.Put)) { + continue; + } + + String key = event.getCurrent().getKey(); + String value = event.getCurrent().getValue(); + if (Strings.isNullOrEmpty(key)) { + continue; + } + resultMap.put(key, value); + } + return resultMap; + } + + @Override + public LockResult lock(String key, long ttl) { + while (true) { Review Comment: 如果是 deprecated,就直接删除吧 -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
