1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.infra.instance;
19
20 import lombok.AccessLevel;
21 import lombok.Getter;
22 import lombok.RequiredArgsConstructor;
23 import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
24 import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
25 import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
26 import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
27 import org.apache.shardingsphere.infra.instance.workerid.WorkerIdGenerator;
28 import org.apache.shardingsphere.infra.lock.LockContext;
29 import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
30
31 import java.util.Collection;
32 import java.util.LinkedHashMap;
33 import java.util.LinkedList;
34 import java.util.Map;
35 import java.util.Optional;
36 import java.util.Properties;
37
38
39
40
41 @RequiredArgsConstructor
42 @Getter
43 public final class InstanceContext {
44
45 private final ComputeNodeInstance instance;
46
47 @Getter(AccessLevel.NONE)
48 private final WorkerIdGenerator workerIdGenerator;
49
50 private final ModeConfiguration modeConfiguration;
51
52 private final ModeContextManager modeContextManager;
53
54 @SuppressWarnings("rawtypes")
55 private final LockContext lockContext;
56
57 private final EventBusContext eventBusContext;
58
59 private final Collection<ComputeNodeInstance> allClusterInstances = new LinkedList<>();
60
61
62
63
64
65
66
67 public void updateInstanceStatus(final String instanceId, final String status) {
68 if (instance.getMetaData().getId().equals(instanceId)) {
69 instance.switchState(status);
70 }
71 updateRelatedComputeNodeInstancesStatus(instanceId, status);
72 }
73
74 private void updateRelatedComputeNodeInstancesStatus(final String instanceId, final String status) {
75 for (ComputeNodeInstance each : allClusterInstances) {
76 if (each.getMetaData().getId().equals(instanceId)) {
77 each.switchState(status);
78 }
79 }
80 }
81
82
83
84
85
86
87
88 public void updateWorkerId(final String instanceId, final Integer workerId) {
89 if (instance.getMetaData().getId().equals(instanceId)) {
90 instance.setWorkerId(workerId);
91 }
92 allClusterInstances.stream().filter(each -> each.getMetaData().getId().equals(instanceId)).forEach(each -> each.setWorkerId(workerId));
93 }
94
95
96
97
98
99
100
101 public void updateLabel(final String instanceId, final Collection<String> labels) {
102 if (instance.getMetaData().getId().equals(instanceId)) {
103 instance.setLabels(labels);
104 }
105 allClusterInstances.stream().filter(each -> each.getMetaData().getId().equals(instanceId)).forEach(each -> each.setLabels(labels));
106 }
107
108
109
110
111
112
113 public int getWorkerId() {
114 return instance.getWorkerId();
115 }
116
117
118
119
120
121
122
123 public int generateWorkerId(final Properties props) {
124 int result = workerIdGenerator.generate(props);
125 instance.setWorkerId(result);
126 return result;
127 }
128
129
130
131
132
133
134 public void addComputeNodeInstance(final ComputeNodeInstance instance) {
135 allClusterInstances.removeIf(each -> each.getMetaData().getId().equalsIgnoreCase(instance.getMetaData().getId()));
136 allClusterInstances.add(instance);
137 }
138
139
140
141
142
143
144 public void deleteComputeNodeInstance(final ComputeNodeInstance instance) {
145 allClusterInstances.removeIf(each -> each.getMetaData().getId().equalsIgnoreCase(instance.getMetaData().getId()));
146 }
147
148
149
150
151
152
153
154
155 public Map<String, InstanceMetaData> getAllClusterInstances(final InstanceType instanceType, final Collection<String> labels) {
156 Map<String, InstanceMetaData> result = new LinkedHashMap<>(allClusterInstances.size(), 1F);
157 for (ComputeNodeInstance each : allClusterInstances) {
158 if (each.getMetaData().getType() == instanceType && labels.stream().anyMatch(((Collection<String>) each.getLabels())::contains)) {
159 result.put(each.getMetaData().getId(), each.getMetaData());
160 }
161 }
162 return result;
163 }
164
165
166
167
168
169
170
171 public Optional<ComputeNodeInstance> getComputeNodeInstanceById(final String instanceId) {
172 return allClusterInstances.stream().filter(each -> instanceId.equals(each.getCurrentInstanceId())).findFirst();
173 }
174
175
176
177
178
179
180 public boolean isCluster() {
181 return "Cluster".equals(modeConfiguration.getType());
182 }
183 }