forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
springliao
committed
Jul 26, 2020
1 parent
3b5365c
commit e90ac7e
Showing
12 changed files
with
386 additions
and
136 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
117 changes: 117 additions & 0 deletions
117
naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/PersistentNotifier.java
This file contains 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,117 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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.alibaba.nacos.naming.consistency.persistent; | ||
|
||
import com.alibaba.nacos.common.notify.Event; | ||
import com.alibaba.nacos.common.notify.listener.Subscriber; | ||
import com.alibaba.nacos.common.utils.ConcurrentHashSet; | ||
import com.alibaba.nacos.naming.consistency.ApplyAction; | ||
import com.alibaba.nacos.naming.consistency.KeyBuilder; | ||
import com.alibaba.nacos.naming.consistency.RecordListener; | ||
import com.alibaba.nacos.naming.misc.Loggers; | ||
import com.alibaba.nacos.naming.pojo.Record; | ||
import org.jboss.netty.util.internal.ConcurrentHashMap; | ||
|
||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a> | ||
*/ | ||
public final class PersistentNotifier extends Subscriber<ValueChangeEvent> { | ||
|
||
private final Map<String, ConcurrentHashSet<RecordListener>> listenerMap = new ConcurrentHashMap<>(); | ||
|
||
private final Function<String, Record> find; | ||
|
||
public PersistentNotifier(Function<String, Record> find) { | ||
this.find = find; | ||
} | ||
|
||
public void registerListener(final String key, final RecordListener listener) { | ||
listenerMap.computeIfAbsent(key, s -> new ConcurrentHashSet<>()); | ||
listenerMap.get(key).add(listener); | ||
} | ||
|
||
public void deregisterListener(final String key, final RecordListener listener) { | ||
if (!listenerMap.containsKey(key)) { | ||
return; | ||
} | ||
listenerMap.get(key).remove(listener); | ||
} | ||
|
||
public void addTask(final String key, final ApplyAction action) { | ||
|
||
} | ||
|
||
public int tasksSize() { | ||
return 0; | ||
} | ||
|
||
public <T extends Record> void notify(final String key, final ApplyAction action, final T value) { | ||
if (listenerMap.containsKey(KeyBuilder.SERVICE_META_KEY_PREFIX)) { | ||
|
||
if (KeyBuilder.matchServiceMetaKey(key) && !KeyBuilder.matchSwitchKey(key)) { | ||
|
||
for (RecordListener listener : listenerMap.get(KeyBuilder.SERVICE_META_KEY_PREFIX)) { | ||
try { | ||
if (action == ApplyAction.CHANGE) { | ||
listener.onChange(key, value); | ||
} | ||
|
||
if (action == ApplyAction.DELETE) { | ||
listener.onDelete(key); | ||
} | ||
} catch (Throwable e) { | ||
Loggers.RAFT | ||
.error("[NACOS-RAFT] error while notifying listener of key: {}", key, | ||
e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (!listenerMap.containsKey(key)) { | ||
return; | ||
} | ||
|
||
for (RecordListener listener : listenerMap.get(key)) { | ||
try { | ||
if (action == ApplyAction.CHANGE) { | ||
listener.onChange(key, value); | ||
continue; | ||
} | ||
|
||
if (action == ApplyAction.DELETE) { | ||
listener.onDelete(key); | ||
} | ||
} catch (Throwable e) { | ||
Loggers.RAFT.error("[NACOS-RAFT] error while notifying listener of key: {}", key, e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void onEvent(ValueChangeEvent event) { | ||
notify(event.getKey(), event.getAction(), find.apply(event.getKey())); | ||
} | ||
|
||
@Override | ||
public Class<? extends Event> subscribeType() { | ||
return ValueChangeEvent.class; | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/ValueChangeEvent.java
This file contains 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,86 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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.alibaba.nacos.naming.consistency.persistent; | ||
|
||
import com.alibaba.nacos.common.notify.Event; | ||
import com.alibaba.nacos.naming.consistency.ApplyAction; | ||
import com.alibaba.nacos.naming.pojo.Record; | ||
|
||
/** | ||
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a> | ||
*/ | ||
public class ValueChangeEvent extends Event { | ||
|
||
private final String key; | ||
|
||
private final Record value; | ||
|
||
private final ApplyAction action; | ||
|
||
public ValueChangeEvent(String key, Record value, ApplyAction action) { | ||
this.key = key; | ||
this.value = value; | ||
this.action = action; | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
|
||
public Record getValue() { | ||
return value; | ||
} | ||
|
||
public ApplyAction getAction() { | ||
return action; | ||
} | ||
|
||
public static final class ValueChangeEventBuilder { | ||
|
||
private String key; | ||
|
||
private Record value; | ||
|
||
private ApplyAction action; | ||
|
||
private ValueChangeEventBuilder() { | ||
} | ||
|
||
public static ValueChangeEventBuilder aValueChangeEvent() { | ||
return new ValueChangeEventBuilder(); | ||
} | ||
|
||
public ValueChangeEventBuilder key(String key) { | ||
this.key = key; | ||
return this; | ||
} | ||
|
||
public ValueChangeEventBuilder value(Record value) { | ||
this.value = value; | ||
return this; | ||
} | ||
|
||
public ValueChangeEventBuilder action(ApplyAction action) { | ||
this.action = action; | ||
return this; | ||
} | ||
|
||
public ValueChangeEvent build() { | ||
return new ValueChangeEvent(key, value, action); | ||
} | ||
} | ||
} |
This file contains 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
Oops, something went wrong.