-
Notifications
You must be signed in to change notification settings - Fork 635
[ISSUE #1028] support Etcd Registry #1029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d751aea
update: update grpc version from 1.15.0 to 1.17.1
c85d152
update: update Generated grpc version from 1.15.0 to 1.17.1
439d056
add: support Etcd Registry
0c82b44
feat: change EtcdClientFactory to singleton
ce7c8c6
add: jetcd-core-0.3.0.jar to known-dependencies.txt
5145935
feat: throw exception when createClient failed
ceefd1e
fix: move EtcdClientWrapper into factory package
6aa086f
add: new known-dependencies
55cfdbe
fix: checkStyle formatted
9c2837b
fix: EtcdRegistryServiceTest param
53e8e07
fix: add EtcdRegistryMonitorExecutorService & fix logger format
087ec48
fix: add comment
28d4c19
fix: the unit test code
909c638
fix: unit test code add RegistryException expected
f281747
fix: EtcdRegistryService add shutdown executor
16a2862
remove: EtcdRegistryServiceTest nnecessary stubbings
2e94c9a
fix: EtcdRegistryServiceTest checkStyle
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
eventmesh-registry-plugin/eventmesh-registry-etcd/build.gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
dependencies { | ||
implementation ("io.etcd:jetcd-core:0.3.0") | ||
implementation project(":eventmesh-registry-plugin:eventmesh-registry-api") | ||
implementation project(":eventmesh-common") | ||
testImplementation "org.mockito:mockito-core" | ||
} |
19 changes: 19 additions & 0 deletions
19
eventmesh-registry-plugin/eventmesh-registry-etcd/gradle.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
pluginType=registry | ||
pluginName=etcd |
36 changes: 36 additions & 0 deletions
36
...registry-etcd/src/main/java/org/apache/eventmesh/registry/etcd/constant/EtcdConstant.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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.eventmesh.registry.etcd.constant; | ||
|
||
/** | ||
* EtcdConstant. | ||
*/ | ||
public class EtcdConstant { | ||
|
||
public static final String SERVER_ADDR = "serverAddr"; | ||
|
||
public static final String USERNAME = "username"; | ||
|
||
public static final String PASSWORD = "password"; | ||
|
||
public static final String KEY_SEPARATOR = "/"; | ||
|
||
public static final long TTL = 15L; | ||
|
||
|
||
} |
116 changes: 116 additions & 0 deletions
116
...stry-etcd/src/main/java/org/apache/eventmesh/registry/etcd/factory/EtcdClientFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* 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.eventmesh.registry.etcd.factory; | ||
|
||
import org.apache.eventmesh.api.exception.RegistryException; | ||
import org.apache.eventmesh.common.Constants; | ||
import org.apache.eventmesh.registry.etcd.constant.EtcdConstant; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Properties; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.etcd.jetcd.ByteSequence; | ||
import io.etcd.jetcd.Client; | ||
import io.etcd.jetcd.ClientBuilder; | ||
import io.etcd.jetcd.options.LeaseOption; | ||
|
||
|
||
public class EtcdClientFactory { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(EtcdClientFactory.class); | ||
|
||
private static final Map<String, EtcdLeaseId> etcdLeaseIdMap = new ConcurrentHashMap<>(); | ||
|
||
|
||
public static Client createClient(Properties properties) { | ||
String serverAddr = properties.getProperty(EtcdConstant.SERVER_ADDR); | ||
String username = properties.getProperty(EtcdConstant.USERNAME); | ||
String password = properties.getProperty(EtcdConstant.PASSWORD); | ||
|
||
EtcdLeaseId etcdLeaseId = etcdLeaseIdMap.get(serverAddr); | ||
if (Objects.nonNull(etcdLeaseId)) { | ||
return etcdLeaseId.getClientWrapper(); | ||
} | ||
ClientBuilder clientBuilder = Client.builder(); | ||
String[] addresses = serverAddr.split(","); | ||
String[] httpAddress = new String[addresses.length]; | ||
for (int i = 0; i < addresses.length; i++) { | ||
if (!addresses[i].startsWith(Constants.HTTP_PROTOCOL_PREFIX)) { | ||
httpAddress[i] = Constants.HTTP_PROTOCOL_PREFIX + addresses[i]; | ||
} | ||
} | ||
etcdLeaseId = new EtcdLeaseId(); | ||
try { | ||
etcdLeaseId.setUrl(serverAddr); | ||
etcdLeaseId.setClientBuilder(clientBuilder.endpoints(httpAddress)); | ||
if (StringUtils.isNoneBlank(username)) { | ||
etcdLeaseId.getClientBuilder().user(ByteSequence.from(username.getBytes())); | ||
} | ||
if (StringUtils.isNoneBlank(password)) { | ||
etcdLeaseId.getClientBuilder().password(ByteSequence.from(password.getBytes())); | ||
} | ||
etcdLeaseId.setClientWrapper(new EtcdClientWrapper(etcdLeaseId.getClientBuilder().build())); | ||
EtcdClientWrapper client = etcdLeaseId.getClientWrapper(); | ||
long leaseId = client.getLeaseClient().grant(EtcdConstant.TTL).get().getID(); | ||
etcdLeaseId.setLeaseId(leaseId); | ||
EtcdStreamObserver etcdStreamObserver = new EtcdStreamObserver(); | ||
etcdStreamObserver.setEtcdLeaseId(etcdLeaseId); | ||
etcdLeaseId.setEtcdStreamObserver(etcdStreamObserver); | ||
client.getLeaseClient().keepAlive(leaseId, etcdStreamObserver); | ||
|
||
etcdLeaseIdMap.put(serverAddr, etcdLeaseId); | ||
} catch (Throwable e) { | ||
logger.error("createClient failed, address: {}", serverAddr, e); | ||
throw new RegistryException("createClient failed", e); | ||
} | ||
return etcdLeaseId.getClientWrapper(); | ||
} | ||
|
||
|
||
public static void renewalLeaseId(EtcdLeaseId etcdLeaseId) { | ||
logger.info("renewal of contract. server url: {}", etcdLeaseId.getUrl()); | ||
Client client = etcdLeaseId.getClientWrapper(); | ||
try { | ||
long ttl = client.getLeaseClient().timeToLive(etcdLeaseId.getLeaseId(), LeaseOption.DEFAULT).get().getTTl(); | ||
if (ttl < 1) { | ||
long leaseId = client.getLeaseClient().grant(EtcdConstant.TTL).get().getID(); | ||
client.getLeaseClient().keepAlive(leaseId, etcdLeaseId.getEtcdStreamObserver()); | ||
etcdLeaseId.setLeaseId(leaseId); | ||
} | ||
} catch (Throwable e) { | ||
logger.error("renewal error, server url: {}", etcdLeaseId.getUrl(), e); | ||
client.getLeaseClient().keepAlive(System.currentTimeMillis(), etcdLeaseId.getEtcdStreamObserver()); | ||
} | ||
} | ||
|
||
public static Long getLeaseId(String url) { | ||
return getEtcdLeaseId(url).getLeaseId(); | ||
} | ||
|
||
public static EtcdLeaseId getEtcdLeaseId(String url) { | ||
return etcdLeaseIdMap.get(url); | ||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
...stry-etcd/src/main/java/org/apache/eventmesh/registry/etcd/factory/EtcdClientWrapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* 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.eventmesh.registry.etcd.factory; | ||
|
||
|
||
import io.etcd.jetcd.Auth; | ||
import io.etcd.jetcd.Client; | ||
import io.etcd.jetcd.Cluster; | ||
import io.etcd.jetcd.KV; | ||
import io.etcd.jetcd.Lease; | ||
import io.etcd.jetcd.Lock; | ||
import io.etcd.jetcd.Maintenance; | ||
import io.etcd.jetcd.Watch; | ||
|
||
|
||
class EtcdClientWrapper implements Client { | ||
|
||
private volatile Client client; | ||
|
||
public EtcdClientWrapper(Client client) { | ||
this.client = client; | ||
} | ||
|
||
@Override | ||
public Auth getAuthClient() { | ||
return client.getAuthClient(); | ||
} | ||
|
||
@Override | ||
public KV getKVClient() { | ||
return client.getKVClient(); | ||
} | ||
|
||
@Override | ||
public Cluster getClusterClient() { | ||
return client.getClusterClient(); | ||
} | ||
|
||
@Override | ||
public Maintenance getMaintenanceClient() { | ||
return client.getMaintenanceClient(); | ||
} | ||
|
||
@Override | ||
public Lease getLeaseClient() { | ||
return client.getLeaseClient(); | ||
} | ||
|
||
@Override | ||
public Watch getWatchClient() { | ||
return client.getWatchClient(); | ||
} | ||
|
||
@Override | ||
public Lock getLockClient() { | ||
return client.getLockClient(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
client.close(); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.