Skip to content

Commit

Permalink
[app_discovery] app revision meta report
Browse files Browse the repository at this point in the history
  • Loading branch information
dzdx committed Dec 16, 2020
1 parent 0810921 commit 58a06ac
Show file tree
Hide file tree
Showing 26 changed files with 1,349 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 com.alipay.sofa.registry.core.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AppRevisionInterface implements Serializable {
public String dataId;
public String group;
public String instanceId;
public Map<String, List<String>> serviceParams;

public AppRevisionInterface() {
serviceParams = new HashMap<>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 com.alipay.sofa.registry.core.model;

import java.io.Serializable;
import java.util.Objects;

public class AppRevisionKey implements Comparable<AppRevisionKey>, Serializable {
String appname;
String revision;

public AppRevisionKey(String appname, String revision) {
this.appname = appname;
this.revision = revision;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
AppRevisionKey that = (AppRevisionKey) o;
return Objects.equals(appname, that.appname) && Objects.equals(revision, that.revision);
}

@Override
public int hashCode() {
return Objects.hash(appname, revision);
}

@Override
public String toString() {
return appname + "@" + revision;
}

@Override
public int compareTo(AppRevisionKey o) {
return toString().compareTo(o.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 com.alipay.sofa.registry.core.model;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class AppRevisionRegister implements Serializable {
public String revision;
public String appname;
public Map<String, String> baseParams;
public List<AppRevisionInterface> interfaces;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 com.alipay.sofa.registry.common.model.metaserver;

import java.io.Serializable;

public class CheckRevisionsRequest implements Serializable {
public String keysDigest;

public CheckRevisionsRequest(String keysDigest) {
this.keysDigest = keysDigest;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 com.alipay.sofa.registry.common.model.metaserver;

import com.alipay.sofa.registry.core.model.AppRevisionKey;

import java.io.Serializable;
import java.util.List;

public class FetchRevisionsRequest implements Serializable {
public List<AppRevisionKey> keys;

public FetchRevisionsRequest(List<AppRevisionKey> keys) {
this.keys = keys;
}
}
8 changes: 8 additions & 0 deletions server/common/util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 com.alipay.sofa.registry.util;

import com.alipay.sofa.registry.core.model.AppRevisionKey;
import org.springframework.util.DigestUtils;

import java.util.Collections;
import java.util.List;

public class RevisionUtils {
public static String revisionsDigest(List<AppRevisionKey> keys) {
Collections.sort(keys);
StringBuffer sb = new StringBuffer();
for (AppRevisionKey key : keys) {
sb.append(key.toString());
}
return DigestUtils.md5DigestAsHex(sb.toString().getBytes());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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 com.alipay.sofa.registry.util;

import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* SingleFlight implements call deduplication for equal keys.
* <p/>
* Example:
* <pre>
* public Result expensiveOperation(final Parameters parameters) throws Exception {
* return singleFlight.execute(parameters, new Callable&lt;Result&gt;() {
* &#64;Override
* public Result call() {
* return expensiveOperationImpl(parameters);
* }
* });
* }
*
* private Result expensiveOperationImpl(Parameters parameters) {
* // the real implementation
* }
* </pre>
*/
public class SingleFlight {

private final ConcurrentMap<Object, Call> calls = new ConcurrentHashMap<>();

/**
* Execute a {@link Callable} if no other calls for the same {@code key} are currently running.
* Concurrent calls for the same {@code key} result in one caller invoking the {@link Callable} and sharing the result
* with the other callers.
* <p/>
* The result of an invocation is not cached, only concurrent calls share the same result.
*
* @param key A unique identification of the method call.
* The {@code key} must be uniquely identifiable by it's {@link Object#hashCode()} and {@link Object#equals(Object)} methods.
* @param callable The {@link Callable} where the result can be obtained from.
* @return The result of invoking the {@link Callable}.
* @throws Exception The {@link Exception} which was thrown by the {@link Callable}.
* Alternatively a {@link InterruptedException} can be thrown if
* the executing {@link Thread} was interrupted while waiting for the result.
*/
@SuppressWarnings("unchecked")
public <V> V execute(Object key, Callable<V> callable) throws Exception {
Call<V> call = calls.get(key);
if (call == null) {
call = new Call<>();
Call<V> other = calls.putIfAbsent(key, call);
if (other == null) {
try {
return call.exec(callable);
} finally {
calls.remove(key);
}
} else {
call = other;
}
}
return call.await();
}

private static class Call<V> {

private final Object lock = new Object();
private boolean finished;
private V result;
private Exception exc;

void finished(V result, Exception exc) {
synchronized (lock) {
this.finished = true;
this.result = result;
this.exc = exc;
lock.notifyAll();
}
}

V await() throws Exception {
synchronized (lock) {
while (!finished) {
lock.wait();
}
if (exc != null) {
throw exc;
}
return result;
}
}

V exec(Callable<V> callable) throws Exception {
V result = null;
Exception exc = null;
try {
result = callable.call();
return result;
} catch (Exception e) {
exc = e;
throw e;
} finally {
finished(result, exc);
}
}
}
}
38 changes: 38 additions & 0 deletions server/consistency/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>registry-server-parent</artifactId>
<version>5.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>registry-consistency</artifactId>

<properties>
<main.user.dir>../../</main.user.dir>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


</project>
Loading

0 comments on commit 58a06ac

Please sign in to comment.