Skip to content

Commit

Permalink
[app-discovery] filter databox by revision
Browse files Browse the repository at this point in the history
  • Loading branch information
dzdx committed Dec 16, 2020
1 parent 5d646aa commit aa1d36a
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,80 @@
import java.util.Map;

public class AppRevisionInterface implements Serializable {
public String dataId;
public String group;
public String instanceId;
public Map<String, List<String>> serviceParams;
private String dataId;
private String group;
private String instanceId;
private Map<String, List<String>> serviceParams = new HashMap<>();

public AppRevisionInterface() {
serviceParams = new HashMap<>();
/**
* Getter method for property <tt>dataId</tt>.
*
* @return property value of dataId
*/
public String getDataId() {
return dataId;
}

/**
* Setter method for property <tt>dataId</tt>.
*
* @param dataId value to be assigned to property dataId
*/
public void setDataId(String dataId) {
this.dataId = dataId;
}

/**
* Getter method for property <tt>group</tt>.
*
* @return property value of group
*/
public String getGroup() {
return group;
}

/**
* Setter method for property <tt>group</tt>.
*
* @param group value to be assigned to property group
*/
public void setGroup(String group) {
this.group = group;
}

/**
* Getter method for property <tt>instanceId</tt>.
*
* @return property value of instanceId
*/
public String getInstanceId() {
return instanceId;
}

/**
* Setter method for property <tt>instanceId</tt>.
*
* @param instanceId value to be assigned to property instanceId
*/
public void setInstanceId(String instanceId) {
this.instanceId = instanceId;
}

/**
* Getter method for property <tt>serviceParams</tt>.
*
* @return property value of serviceParams
*/
public Map<String, List<String>> getServiceParams() {
return serviceParams;
}

/**
* Setter method for property <tt>serviceParams</tt>.
*
* @param serviceParams value to be assigned to property serviceParams
*/
public void setServiceParams(Map<String, List<String>> serviceParams) {
this.serviceParams = serviceParams;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,80 @@
import java.util.Map;

public class AppRevisionRegister implements Serializable {
public String revision;
public String appname;
public Map<String, List<String>> baseParams;
public Map<String, AppRevisionInterface> interfaces;
private String revision;
private String appName;
private Map<String, List<String>> baseParams;
private Map<String, AppRevisionInterface> interfaces;

/**
* Getter method for property <tt>revision</tt>.
*
* @return property value of revision
*/
public String getRevision() {
return revision;
}

/**
* Setter method for property <tt>revision</tt>.
*
* @param revision value to be assigned to property revision
*/
public void setRevision(String revision) {
this.revision = revision;
}

/**
* Getter method for property <tt>appName</tt>.
*
* @return property value of appName
*/
public String getAppName() {
return appName;
}

/**
* Setter method for property <tt>appName</tt>.
*
* @param appName value to be assigned to property appName
*/
public void setAppName(String appName) {
this.appName = appName;
}

/**
* Getter method for property <tt>baseParams</tt>.
*
* @return property value of baseParams
*/
public Map<String, List<String>> getBaseParams() {
return baseParams;
}

/**
* Setter method for property <tt>baseParams</tt>.
*
* @param baseParams value to be assigned to property baseParams
*/
public void setBaseParams(Map<String, List<String>> baseParams) {
this.baseParams = baseParams;
}

/**
* Getter method for property <tt>interfaces</tt>.
*
* @return property value of interfaces
*/
public Map<String, AppRevisionInterface> getInterfaces() {
return interfaces;
}

/**
* Setter method for property <tt>interfaces</tt>.
*
* @param interfaces value to be assigned to property interfaces
*/
public void setInterfaces(Map<String, AppRevisionInterface> interfaces) {
this.interfaces = interfaces;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.alipay.sofa.registry.common.model;

import com.alipay.sofa.registry.common.model.store.WordCache;
import com.google.common.collect.ArrayListMultimap;

import java.io.Serializable;
import java.util.HashMap;
Expand All @@ -42,24 +41,7 @@ public class AppRegisterServerDataBox implements Serializable {
private HashMap<String/*key*/, List<String>/*values*/> baseParams;

/** */
private Map<String/*service*/, Map<String/*key*/, List<String>/*value*/>> serviceParams;

public String extract(String serviceName) {
serviceParams.get(serviceName);

StringBuilder builder = new StringBuilder();
builder.append("?");
baseParams.entrySet().stream().forEach(entry -> {
entry.getValue().forEach(value -> builder.append(entry.getKey()).append("=").append(value).append("&"));
});

serviceParams.get(serviceName).entrySet().forEach(entry -> {
entry.getValue().forEach(value -> builder.append(entry.getKey()).append("=").append(value).append("&"));
});


return builder.deleteCharAt(builder.toString().length() - 1).toString();
}
private Map<String/*service*/, Map<String/*key*/, List<String>/*value*/>> interfaceParams;

/**
* Getter method for property <tt>revision</tt>.
Expand Down Expand Up @@ -116,20 +98,20 @@ public void setBaseParams(HashMap<String, List<String>> baseParams) {
}

/**
* Getter method for property <tt>serviceParams</tt>.
* Getter method for property <tt>interfaceParams</tt>.
*
* @return property value of serviceParams
* @return property value of interfaceParams
*/
public Map<String, Map<String, List<String>>> getServiceParams() {
return serviceParams;
public Map<String, Map<String, List<String>>> getInterfaceParams() {
return interfaceParams;
}

/**
* Setter method for property <tt>serviceParams</tt>.
* Setter method for property <tt>interfaceParams</tt>.
*
* @param serviceParams value to be assigned to property serviceParams
* @param interfaceParams value to be assigned to property interfaceParams
*/
public void setServiceParams(Map<String, Map<String, List<String>>> serviceParams) {
this.serviceParams = serviceParams;
public void setInterfaceParams(Map<String, Map<String, List<String>>> interfaceParams) {
this.interfaceParams = interfaceParams;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
*
Expand Down Expand Up @@ -66,7 +65,7 @@ public static Publisher internPublisher(Publisher publisher) {
});

Map<String, Map<String, List<String>>> serviceParams = new HashMap<>();
dataBox.getServiceParams().entrySet().forEach(entry -> {
dataBox.getInterfaceParams().entrySet().forEach(entry -> {
// cache serviceName
serviceParams.put(WordCache.getInstance().getWordCache(entry.getKey()), entry.getValue());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public boolean load(String path) {
return true;
} catch (IOException e) {
LOGGER.error("Load app revisions error:", e);
e.printStackTrace();
return false;
}
}
Expand All @@ -95,7 +94,7 @@ public Set<String> getSnapshotFileNames() {

public void add(AppRevisionRegister appRevision) {
rwLock.writeLock().lock();
if (registry.putIfAbsent(appRevision.revision, appRevision) == null) {
if (registry.putIfAbsent(appRevision.getRevision(), appRevision) == null) {
keysDigest = generateKeysDigest();
}
rwLock.writeLock().unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class AppRevisionRegistry {
private AppRevisionService appRevisionService;

public void register(AppRevisionRegister appRevision) {
if (appRevisionService.existed(appRevision.revision)) {
if (appRevisionService.existed(appRevision.getRevision())) {
return;
}
appRevisionService.add(appRevision);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ private Datum doMerge(Datum interfaceDatum, Map<String, Datum> appDatum, Subscri
continue;
}
AppPublisher appPublisher = (AppPublisher) publisher;

datum.getPubMap().put(
appPublisher.getRegisterId(),
AppPublisherConverter.convert(appPublisher, appRevisionCacheRegistry,
dataInfo));
Publisher newPublisher = AppPublisherConverter.convert(appPublisher,
appRevisionCacheRegistry, dataInfo);
if (newPublisher.getDataList().size() > 0) {
datum.getPubMap().put(appPublisher.getRegisterId(), newPublisher);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,20 @@ public AppRevisionCacheRegistry() {
}

public void register(AppRevisionRegister appRevision) throws Exception {
if (this.registry.containsKey(appRevision.revision)) {
if (this.registry.containsKey(appRevision.getRevision())) {
return;
}
singleFlight.execute("revisionRegister" + appRevision.revision, () -> {
singleFlight.execute("revisionRegister" + appRevision.getRevision(), () -> {
appRevisionNodeService.register(appRevision);
return null;
});
}

public Set<String> getApps(String dataInfoId) {
public Map<String, Set<String>> getAppRevisions(String dataInfoId) {
if (!interfaceRevisions.containsKey(dataInfoId)) {
return new HashSet<>();
return new HashMap<>();
}
return interfaceRevisions.get(dataInfoId).keySet();
return interfaceRevisions.get(dataInfoId);
}

public AppRevisionRegister getRevision(String revision) {
Expand Down Expand Up @@ -98,18 +98,18 @@ public void refreshAll() {
}

private void onNewRevision(AppRevisionRegister rev) {
for (AppRevisionInterface inf : rev.interfaces.values()) {
String dataInfoId = DataInfo.toDataInfoId(inf.dataId, inf.instanceId, inf.group);
for (AppRevisionInterface inf : rev.getInterfaces().values()) {
String dataInfoId = DataInfo.toDataInfoId(inf.getDataId(), inf.getInstanceId(), inf.getGroup());
Map<String, Set<String>> apps = interfaceRevisions.computeIfAbsent(dataInfoId,
k -> new ConcurrentHashMap<>());
Set<String> infRevisions = apps.computeIfAbsent(rev.appname,
Set<String> infRevisions = apps.computeIfAbsent(rev.getAppName(),
k -> Sets.newConcurrentHashSet());
infRevisions.add(rev.revision);
infRevisions.add(rev.getRevision());

appInterfaces.computeIfAbsent(rev.appname, k -> Sets.newConcurrentHashSet())
appInterfaces.computeIfAbsent(rev.getAppName(), k -> Sets.newConcurrentHashSet())
.add(dataInfoId);
}
registry.put(rev.revision, rev);
registry.put(rev.getRevision(), rev);
}

private String generateKeysDigest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -112,7 +113,7 @@ public Map<String, Datum> getDatumsCache(String dataInfoId) {
public Map<String, Map<String, Datum>> getAppDatumCache(String dataInfoId, String instanceId) {
Map<String/*datacenter*/, Map<String/*appName*/, Datum>> result = new HashMap<>();
//get metadata from session cache
for (String appName : appRevisionCacheRegistry.getApps(dataInfoId)) {
for (String appName : appRevisionCacheRegistry.getAppRevisions(dataInfoId).keySet()) {
String appDataInfoId = DataInfo.toDataInfoId(appName, instanceId, ValueConstants.SOFA_APP);

Map<String/*datacenter*/, Datum> appDatum = this.getDatumsCache(appDataInfoId);
Expand Down
Loading

0 comments on commit aa1d36a

Please sign in to comment.