|
| 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 org.apache.dolphinscheduler.plugin.registry.etcd; |
| 19 | + |
| 20 | +import org.apache.dolphinscheduler.registry.api.RegistryException; |
| 21 | + |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Optional; |
| 24 | +import java.util.concurrent.ConcurrentHashMap; |
| 25 | +import java.util.concurrent.ExecutionException; |
| 26 | + |
| 27 | +import lombok.extern.slf4j.Slf4j; |
| 28 | +import io.etcd.jetcd.Client; |
| 29 | +import io.etcd.jetcd.lease.LeaseKeepAliveResponse; |
| 30 | +import io.grpc.stub.StreamObserver; |
| 31 | + |
| 32 | +@Slf4j |
| 33 | +public class EtcdKeepAliveLeaseManager { |
| 34 | + |
| 35 | + private final Map<String, Long> keyLeaseCache = new ConcurrentHashMap<>(); |
| 36 | + |
| 37 | + private final Client client; |
| 38 | + |
| 39 | + EtcdKeepAliveLeaseManager(Client client) { |
| 40 | + this.client = client; |
| 41 | + } |
| 42 | + |
| 43 | + long getOrCreateKeepAliveLease(String key, long timeToLive) { |
| 44 | + return keyLeaseCache.computeIfAbsent(key, $ -> { |
| 45 | + try { |
| 46 | + long leaseId = client.getLeaseClient().grant(timeToLive).get().getID(); |
| 47 | + client.getLeaseClient().keepAlive(leaseId, new StreamObserver<LeaseKeepAliveResponse>() { |
| 48 | + |
| 49 | + @Override |
| 50 | + public void onNext(LeaseKeepAliveResponse value) { |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void onError(Throwable t) { |
| 55 | + log.error("Lease {} keep alive error, remove cache with key:{}", leaseId, key, t); |
| 56 | + keyLeaseCache.remove(key); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void onCompleted() { |
| 61 | + log.error("Lease {} keep alive complete, remove cache with key:{}", leaseId, key); |
| 62 | + keyLeaseCache.remove(key); |
| 63 | + } |
| 64 | + }); |
| 65 | + log.info("Lease {} keep alive create with key:{}", leaseId, key); |
| 66 | + return leaseId; |
| 67 | + } catch (InterruptedException e) { |
| 68 | + Thread.currentThread().interrupt(); |
| 69 | + throw new RegistryException("Failed to create lease key: " + key, e); |
| 70 | + } catch (ExecutionException e) { |
| 71 | + throw new RegistryException("Failed to create lease key: " + key, e); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + Optional<Long> getKeepAliveLease(String key) { |
| 77 | + return Optional.ofNullable(keyLeaseCache.get(key)); |
| 78 | + } |
| 79 | +} |
0 commit comments