Skip to content

Commit

Permalink
#677 Add ephemeral instance implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nkorange committed Jan 24, 2019
1 parent a77076a commit 0e0900b
Show file tree
Hide file tree
Showing 47 changed files with 770 additions and 1,199 deletions.
22 changes: 20 additions & 2 deletions api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* Instance
*
* @author dungu.zpf
* @author nkorange
*/
public class Instance {

Expand Down Expand Up @@ -54,8 +54,18 @@ public class Instance {
@JSONField(name = "valid")
private boolean healthy = true;

/**
* If instance is enabled to accept request
*/
private boolean enabled = true;

/**
* If instance is ephemeral
*
* @since 1.0.0
*/
private boolean ephemeral = true;

/**
* cluster information of instance
*/
Expand Down Expand Up @@ -147,6 +157,14 @@ public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public boolean isEphemeral() {
return ephemeral;
}

public void setEphemeral(boolean ephemeral) {
this.ephemeral = ephemeral;
}

@Override
public String toString() {
return JSON.toJSONString(this);
Expand All @@ -162,7 +180,7 @@ public boolean equals(Object obj) {
return false;
}

Instance host = (Instance)obj;
Instance host = (Instance) obj;

return strEquals(toString(), host.toString());
}
Expand Down
11 changes: 3 additions & 8 deletions api/src/main/java/com/alibaba/nacos/api/naming/pojo/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public class Service {

private Map<String, String> metadata = new HashMap<String, String>();

public Service() {
}

public Service(String name) {
this.name = name;
}
Expand Down Expand Up @@ -114,12 +117,4 @@ public void setMetadata(Map<String, String> metadata) {
public void addMetadata(String key, String value) {
this.metadata.put(key, value);
}

public AbstractSelector getSelector() {
return selector;
}

public void setSelector(AbstractSelector selector) {
this.selector = selector;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package com.alibaba.nacos.naming.acl;

import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.core.utils.WebUtils;
import com.alibaba.nacos.naming.core.Domain;
import com.alibaba.nacos.naming.core.Service;
import com.alibaba.nacos.naming.core.ServiceManager;
import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
Expand Down Expand Up @@ -72,7 +72,7 @@ public void doAuth(Map<String, String[]> params, HttpServletRequest req) throws
dom = WebUtils.optional(req, "tag", "");
}

Domain domObj = serviceManager.getService(namespaceId, dom);
Service domObj = serviceManager.getService(namespaceId, dom);

if (domObj == null) {
if (!req.getRequestURI().equals(UtilsAndCommons.NACOS_NAMING_CONTEXT + UtilsAndCommons.API_SET_ALL_WEIGHTS)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public interface ConsistencyService {
* @return data related to the key
* @throws NacosException
*/
Object get(String key) throws NacosException;
Datum get(String key) throws NacosException;

/**
* Listen for changes of a data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @author nacos
*/
public interface DataListener {
public interface DataListener<T> {

/**
* Determine if the listener was registered with this key
Expand All @@ -45,12 +45,12 @@ public interface DataListener {
* @param value data of the key
* @throws Exception
*/
void onChange(String key, Object value) throws Exception;
void onChange(String key, T value) throws Exception;

/**
* Action to do if data of target key has been removed
*
* @param key target key
* @param key target key
* @throws Exception
*/
void onDelete(String key) throws Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void remove(String key) throws NacosException {
}

@Override
public Object get(String key) throws NacosException {
public Datum get(String key) throws NacosException {
return persistentConsistencyService.get(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

/**
* @author nkorange
* @since 1.0.0
*/
public class KeyBuilder {

Expand All @@ -25,18 +26,26 @@ public class KeyBuilder {
private static final String PERSISTENT_KEY_PREFIX = "persistent";
private static final String INSTANCE_LIST_KEY_PREFIX = "instanceList";

public static String buildEphemeralInstanceListKey(String namespaceId, String serviceName, String clusterName) {
return namespaceId + KEY_CONNECTOR + PERSISTENT_KEY_PREFIX + KEY_CONNECTOR + INSTANCE_LIST_KEY_PREFIX + KEY_CONNECTOR
+ serviceName + KEY_CONNECTOR + clusterName;
public static String buildEphemeralInstanceListKey(String namespaceId, String serviceName) {
return namespaceId + KEY_CONNECTOR
+ serviceName + KEY_CONNECTOR + EPHEMERAL_KEY_PREFIX + KEY_CONNECTOR + INSTANCE_LIST_KEY_PREFIX;
}

public static String buildPersistentInstanceListKey(String namespaceId, String serviceName, String clusterName) {
return namespaceId + KEY_CONNECTOR + EPHEMERAL_KEY_PREFIX + KEY_CONNECTOR + INSTANCE_LIST_KEY_PREFIX + KEY_CONNECTOR
+ serviceName + KEY_CONNECTOR + clusterName;
public static String buildPersistentInstanceListKey(String namespaceId, String serviceName) {
return namespaceId + KEY_CONNECTOR
+ serviceName + KEY_CONNECTOR + PERSISTENT_KEY_PREFIX + KEY_CONNECTOR + INSTANCE_LIST_KEY_PREFIX;
}

public static String buildInstanceListKey(String namespaceId, String serviceName, boolean ephemeral) {
if (ephemeral) {
return buildEphemeralInstanceListKey(namespaceId, serviceName);
}
return buildPersistentInstanceListKey(namespaceId, serviceName);
}


public static boolean matchEphemeralInstanceListKey(String key) {
return key.contains(KEY_CONNECTOR + EPHEMERAL_KEY_PREFIX + KEY_CONNECTOR + INSTANCE_LIST_KEY_PREFIX + KEY_CONNECTOR);
return key.endsWith(KEY_CONNECTOR + EPHEMERAL_KEY_PREFIX + KEY_CONNECTOR + INSTANCE_LIST_KEY_PREFIX);
}

public static boolean matchInstanceListKey(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void remove(String key) throws NacosException {
}

@Override
public Object get(String key) throws NacosException {
public Datum get(String key) throws NacosException {
return dataStore.get(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class RaftConsistencyServiceImpl implements PersistentConsistencyService
@Override
public void put(String key, Object value) throws NacosException {
try {
raftCore.signalPublish(key, (String) value);
raftCore.signalPublish(key, value);
} catch (Exception e) {
Loggers.RAFT.error("Raft put failed.", e);
throw new NacosException(NacosException.SERVER_ERROR, "Raft put failed, key:" + key + ", value:" + value);
Expand All @@ -41,7 +41,7 @@ public void remove(String key) throws NacosException {
}

@Override
public Object get(String key) throws NacosException {
public Datum get(String key) throws NacosException {
return raftCore.getDatum(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public Thread newThread(Runnable r) {

private volatile Map<String, List<DataListener>> listeners = new ConcurrentHashMap<>();

private volatile ConcurrentMap<String, Datum> datums = new ConcurrentHashMap<String, Datum>();
private volatile ConcurrentMap<String, Datum<?>> datums = new ConcurrentHashMap<>();

@Autowired
private RaftPeerSet peers;
Expand All @@ -115,10 +115,10 @@ public void init() throws Exception {

long start = System.currentTimeMillis();

ConcurrentMap<String, Datum> datumMap = raftStore.loadDatums();
ConcurrentMap<String, Datum<?>> datumMap = raftStore.loadDatums();
if (datumMap != null && !datumMap.isEmpty()) {
datums = datumMap;
for (Map.Entry<String, Datum> entry : datumMap.entrySet()) {
for (Map.Entry<String, Datum<?>> entry : datumMap.entrySet()) {
notifier.addTask(entry.getValue(), ApplyAction.CHANGE);
}
}
Expand Down Expand Up @@ -147,7 +147,7 @@ public Map<String, List<DataListener>> getListeners() {
return listeners;
}

public void signalPublish(String key, String value) throws Exception {
public <T> void signalPublish(String key, T value) throws Exception {

if (!isLeader()) {
JSONObject params = new JSONObject();
Expand All @@ -163,7 +163,7 @@ public void signalPublish(String key, String value) throws Exception {
try {
OPERATE_LOCK.lock();
long start = System.currentTimeMillis();
final Datum datum = new Datum();
final Datum<T> datum = new Datum<>();
datum.key = key;
datum.value = value;
if (getDatum(key) == null) {
Expand Down Expand Up @@ -264,7 +264,7 @@ public Integer onCompleted(Response response) throws Exception {
}
}

public void onPublish(Datum datum, RaftPeer source) throws Exception {
public <T> void onPublish(Datum<T> datum, RaftPeer source) throws Exception {
RaftPeer local = peers.local();
if (StringUtils.isBlank((String) datum.value)) {
Loggers.RAFT.warn("received empty datum");
Expand Down Expand Up @@ -614,7 +614,7 @@ public RaftPeer receivedBeat(JSONObject beat) throws Exception {

Map<String, Integer> receivedKeysMap = new HashMap<String, Integer>(datums.size());

for (Map.Entry<String, Datum> entry : datums.entrySet()) {
for (Map.Entry<String, Datum<?>> entry : datums.entrySet()) {
receivedKeysMap.put(entry.getKey(), 0);
}

Expand Down Expand Up @@ -829,7 +829,7 @@ public static String buildURL(String ip, String api) {
return "http://" + ip + RunningConfig.getContextPath() + api;
}

public Datum getDatum(String key) {
public Datum<?> getDatum(String key) {
return datums.get(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public class RaftStore {
CACHE_DIR = BASE_DIR + File.separator + "data";
}

public synchronized ConcurrentHashMap<String, Datum> loadDatums() throws Exception {
public synchronized ConcurrentHashMap<String, Datum<?>> loadDatums() throws Exception {

ConcurrentHashMap<String, Datum> datums = new ConcurrentHashMap<>(32);
ConcurrentHashMap<String, Datum<?>> datums = new ConcurrentHashMap<>(32);
Datum datum;
long start = System.currentTimeMillis();
for (File cache : listCaches()) {
Expand Down
Loading

0 comments on commit 0e0900b

Please sign in to comment.