Skip to content

Commit bf27bbf

Browse files
authored
Merge pull request #763 from scalecube/update-cluster
Added test, updated pom, added null validation to ServiceEndpoint
2 parents b111898 + ce4c1cb commit bf27bbf

File tree

6 files changed

+140
-53
lines changed

6 files changed

+140
-53
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
<properties>
2525
<jackson.version>2.11.0</jackson.version>
26-
<scalecube-cluster.version>2.5.0-RC2</scalecube-cluster.version>
26+
<scalecube-cluster.version>2.5.0</scalecube-cluster.version>
2727
<scalecube-commons.version>1.0.4</scalecube-commons.version>
2828
<scalecube-benchmarks.version>1.2.2</scalecube-benchmarks.version>
2929
<scalecube-config.version>0.4.3</scalecube-config.version>

services-api/src/main/java/io/scalecube/services/ServiceEndpoint.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public ServiceEndpoint() {}
3737

3838
private ServiceEndpoint(Builder builder) {
3939
this.id = Objects.requireNonNull(builder.id, "ServiceEndpoint.id is required");
40-
this.address = builder.address;
40+
this.address = Objects.requireNonNull(builder.address, "ServiceEndpoint.address is required");
4141
this.contentTypes = Collections.unmodifiableSet(new HashSet<>(builder.contentTypes));
4242
this.tags = Collections.unmodifiableMap(new HashMap<>(builder.tags));
4343
this.serviceRegistrations =
@@ -96,11 +96,7 @@ public void writeExternal(ObjectOutput out) throws IOException {
9696
out.writeUTF(id);
9797

9898
// address
99-
boolean addressExists = address != null;
100-
out.writeBoolean(addressExists);
101-
if (addressExists) {
102-
out.writeUTF(address.toString());
103-
}
99+
out.writeUTF(address.toString());
104100

105101
// contentTypes
106102
out.writeInt(contentTypes.size());
@@ -128,10 +124,7 @@ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundExcept
128124
id = in.readUTF();
129125

130126
// address
131-
boolean addressExists = in.readBoolean();
132-
if (addressExists) {
133-
address = Address.from(in.readUTF());
134-
}
127+
address = Address.from(in.readUTF());
135128

136129
// contentTypes
137130
int contentTypesSize = in.readInt();
@@ -163,41 +156,55 @@ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundExcept
163156
public static class Builder {
164157

165158
private String id;
166-
private Address address;
159+
private Address address = Address.NULL_ADDRESS;
167160
private Set<String> contentTypes = Collections.emptySet();
168161
private Map<String, String> tags = Collections.emptyMap();
169162
private Collection<ServiceRegistration> serviceRegistrations = new ArrayList<>();
170163

171164
private Builder() {}
172165

173166
public Builder id(String id) {
174-
this.id = id;
167+
this.id = Objects.requireNonNull(id, "id");
175168
return this;
176169
}
177170

178171
public Builder address(Address address) {
179-
this.address = address;
172+
this.address = Objects.requireNonNull(address, "address");
180173
return this;
181174
}
182175

183176
public Builder contentTypes(Set<String> contentTypes) {
184-
this.contentTypes = contentTypes;
177+
this.contentTypes = Objects.requireNonNull(contentTypes, "contentTypes");
185178
return this;
186179
}
187180

188181
public Builder tags(Map<String, String> tags) {
189-
this.tags = tags;
182+
this.tags = Objects.requireNonNull(tags, "tags");
190183
return this;
191184
}
192185

186+
/**
187+
* Adds {@code serviceRegistrations} to collection of {@code serviceRegistrations}.
188+
*
189+
* @param serviceRegistrations serviceRegistrations
190+
* @return this builder
191+
*/
193192
public Builder appendServiceRegistrations(
194193
Collection<ServiceRegistration> serviceRegistrations) {
195-
this.serviceRegistrations.addAll(serviceRegistrations);
194+
this.serviceRegistrations.addAll(
195+
Objects.requireNonNull(serviceRegistrations, "serviceRegistrations"));
196196
return this;
197197
}
198198

199+
/**
200+
* Setter for {@code serviceRegistrations}.
201+
*
202+
* @param serviceRegistrations serviceRegistrations
203+
* @return this builder
204+
*/
199205
public Builder serviceRegistrations(Collection<ServiceRegistration> serviceRegistrations) {
200-
this.serviceRegistrations = serviceRegistrations;
206+
this.serviceRegistrations =
207+
Objects.requireNonNull(serviceRegistrations, "serviceRegistrations");
201208
return this;
202209
}
203210

services-api/src/main/java/io/scalecube/services/discovery/api/ServiceDiscoveryEvent.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.scalecube.services.discovery.api;
22

33
import io.scalecube.services.ServiceEndpoint;
4-
import java.util.Optional;
4+
import java.util.Objects;
55
import java.util.StringJoiner;
66

77
public class ServiceDiscoveryEvent {
@@ -18,12 +18,13 @@ public enum Type {
1818
/**
1919
* Constructor.
2020
*
21-
* @param type type
22-
* @param serviceEndpoint service endpoint
21+
* @param type type; not null
22+
* @param serviceEndpoint service endpoint; not null
2323
*/
2424
private ServiceDiscoveryEvent(Type type, ServiceEndpoint serviceEndpoint) {
25-
this.type = type;
26-
this.serviceEndpoint = serviceEndpoint;
25+
this.type = Objects.requireNonNull(type, "ServiceDiscoveryEvent: type");
26+
this.serviceEndpoint =
27+
Objects.requireNonNull(serviceEndpoint, "ServiceDiscoveryEvent: serviceEndpoint");
2728
}
2829

2930
public static ServiceDiscoveryEvent newEndpointAdded(ServiceEndpoint serviceEndpoint) {
@@ -62,9 +63,7 @@ public boolean isEndpointRemoved() {
6263
public String toString() {
6364
return new StringJoiner(", ", ServiceDiscoveryEvent.class.getSimpleName() + "[", "]")
6465
.add("type=" + type)
65-
.add(
66-
"serviceEndpoint="
67-
+ Optional.ofNullable(serviceEndpoint).map(ServiceEndpoint::id).orElse(null))
66+
.add("ServiceEndpoint.id='" + serviceEndpoint.id() + "'")
6867
.toString();
6968
}
7069
}

services-discovery/src/main/java/io/scalecube/services/discovery/ScalecubeServiceDiscovery.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.scalecube.services.discovery;
22

3+
import static io.scalecube.services.discovery.api.ServiceDiscoveryEvent.newEndpointAdded;
4+
import static io.scalecube.services.discovery.api.ServiceDiscoveryEvent.newEndpointLeaving;
5+
import static io.scalecube.services.discovery.api.ServiceDiscoveryEvent.newEndpointRemoved;
6+
37
import io.scalecube.cluster.Cluster;
48
import io.scalecube.cluster.ClusterConfig;
59
import io.scalecube.cluster.ClusterImpl;
@@ -16,6 +20,7 @@
1620
import java.lang.management.ManagementFactory;
1721
import java.nio.ByteBuffer;
1822
import java.util.List;
23+
import java.util.Objects;
1924
import java.util.StringJoiner;
2025
import java.util.concurrent.CopyOnWriteArrayList;
2126
import java.util.function.UnaryOperator;
@@ -35,7 +40,8 @@ public final class ScalecubeServiceDiscovery implements ServiceDiscovery {
3540

3641
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceDiscovery.class);
3742

38-
private ServiceEndpoint serviceEndpoint;
43+
private final ServiceEndpoint serviceEndpoint;
44+
3945
private ClusterConfig clusterConfig;
4046
private Cluster cluster;
4147

@@ -48,8 +54,11 @@ public final class ScalecubeServiceDiscovery implements ServiceDiscovery {
4854
* @param serviceEndpoint service endpoint
4955
*/
5056
public ScalecubeServiceDiscovery(ServiceEndpoint serviceEndpoint) {
51-
this.serviceEndpoint = serviceEndpoint;
52-
this.clusterConfig = ClusterConfig.defaultLanConfig().metadata(serviceEndpoint);
57+
this.serviceEndpoint = Objects.requireNonNull(serviceEndpoint, "serviceEndpoint");
58+
this.clusterConfig =
59+
ClusterConfig.defaultLanConfig()
60+
.memberIdGenerator(serviceEndpoint::id)
61+
.metadata(serviceEndpoint);
5362
}
5463

5564
/**
@@ -177,7 +186,7 @@ private void onMembershipEvent(MembershipEvent membershipEvent) {
177186
ServiceDiscoveryEvent discoveryEvent = toServiceDiscoveryEvent(membershipEvent);
178187
if (discoveryEvent == null) {
179188
LOGGER.warn(
180-
"Not publishing discoveryEvent, discoveryEvent is null, membershipEvent: {}",
189+
"DiscoveryEvent is null, cannot publish it (corresponding membershipEvent: {})",
181190
membershipEvent);
182191
return;
183192
}
@@ -192,18 +201,13 @@ private ServiceDiscoveryEvent toServiceDiscoveryEvent(MembershipEvent membership
192201
ServiceDiscoveryEvent discoveryEvent = null;
193202

194203
if (membershipEvent.isAdded() && membershipEvent.newMetadata() != null) {
195-
discoveryEvent =
196-
ServiceDiscoveryEvent.newEndpointAdded(decodeMetadata(membershipEvent.newMetadata()));
204+
discoveryEvent = newEndpointAdded(decodeMetadata(membershipEvent.newMetadata()));
197205
}
198-
199206
if (membershipEvent.isRemoved() && membershipEvent.oldMetadata() != null) {
200-
discoveryEvent =
201-
ServiceDiscoveryEvent.newEndpointRemoved(decodeMetadata(membershipEvent.oldMetadata()));
207+
discoveryEvent = newEndpointRemoved(decodeMetadata(membershipEvent.oldMetadata()));
202208
}
203-
204209
if (membershipEvent.isLeaving() && membershipEvent.newMetadata() != null) {
205-
discoveryEvent =
206-
ServiceDiscoveryEvent.newEndpointLeaving(decodeMetadata(membershipEvent.newMetadata()));
210+
discoveryEvent = newEndpointLeaving(decodeMetadata(membershipEvent.newMetadata()));
207211
}
208212

209213
return discoveryEvent;

services/src/main/java/io/scalecube/services/Microservices.java

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ public String id() {
164164
}
165165

166166
private static String generateId() {
167-
return Long.toHexString(UUID.randomUUID().getMostSignificantBits() & Long.MAX_VALUE);
167+
UUID uuid = UUID.randomUUID();
168+
return Long.toHexString(uuid.getMostSignificantBits())
169+
+ Long.toHexString(uuid.getLeastSignificantBits());
168170
}
169171

170172
@Override
@@ -205,6 +207,11 @@ private Mono<Microservices> start() {
205207
.map(ServiceInfo::serviceInstance)
206208
.collect(Collectors.toList());
207209

210+
if (transportBootstrap == ServiceTransportBootstrap.NULL_INSTANCE
211+
&& !serviceInstances.isEmpty()) {
212+
LOGGER.warn("[{}] ServiceTransport is not set", this.id());
213+
}
214+
208215
return discoveryBootstrap
209216
.createInstance(serviceEndpointBuilder.build())
210217
.publishOn(scheduler)
@@ -312,13 +319,13 @@ private Mono<Void> processBeforeDestroy() {
312319
public static final class Builder {
313320

314321
private Map<String, String> tags = new HashMap<>();
315-
private List<ServiceProvider> serviceProviders = new ArrayList<>();
322+
private final List<ServiceProvider> serviceProviders = new ArrayList<>();
316323
private ServiceRegistry serviceRegistry = new ServiceRegistryImpl();
317324
private ServiceMethodRegistry methodRegistry = new ServiceMethodRegistryImpl();
318325
private Authenticator<Object> authenticator = new DelegatingAuthenticator();
319326
private ServiceDiscoveryBootstrap discoveryBootstrap = new ServiceDiscoveryBootstrap();
320327
private ServiceTransportBootstrap transportBootstrap = new ServiceTransportBootstrap();
321-
private GatewayBootstrap gatewayBootstrap = new GatewayBootstrap();
328+
private final GatewayBootstrap gatewayBootstrap = new GatewayBootstrap();
322329
private ServiceProviderErrorMapper errorMapper = DefaultErrorMapper.INSTANCE;
323330
private ServiceMessageDataDecoder dataDecoder =
324331
Optional.ofNullable(ServiceMessageDataDecoder.INSTANCE)
@@ -487,33 +494,28 @@ public <A, T> Builder defaultPrincipalMapper(
487494

488495
public static class ServiceDiscoveryBootstrap {
489496

490-
public static final Function<ServiceEndpoint, ServiceDiscovery> NULL_FACTORY = i -> null;
491-
492497
private final Function<ServiceEndpoint, ServiceDiscovery> factory;
493498

494499
private ServiceDiscovery discovery;
495500
private Disposable disposable;
496501

497502
private ServiceDiscoveryBootstrap() {
498-
this(NULL_FACTORY);
503+
this(NullServiceDiscovery::new);
499504
}
500505

501506
private ServiceDiscoveryBootstrap(Function<ServiceEndpoint, ServiceDiscovery> factory) {
502507
this.factory = factory;
503508
}
504509

505510
private Mono<ServiceDiscovery> createInstance(ServiceEndpoint serviceEndpoint) {
506-
return factory == NULL_FACTORY
507-
? Mono.empty()
508-
: Mono.defer(() -> Mono.just(discovery = factory.apply(serviceEndpoint)));
511+
return Mono.fromCallable(() -> discovery = factory.apply(serviceEndpoint));
509512
}
510513

511514
private Mono<ServiceDiscovery> startListen(Microservices microservices) {
512515
return Mono.defer(
513516
() -> {
514-
if (discovery == null) {
515-
LOGGER.info("[{}] ServiceDiscovery not set", microservices.id());
516-
return Mono.empty();
517+
if (discovery instanceof NullServiceDiscovery) {
518+
return Mono.just(discovery);
517519
}
518520

519521
disposable =
@@ -561,6 +563,40 @@ private Mono<Void> shutdown() {
561563
}
562564
}
563565

566+
private static class NullServiceDiscovery implements ServiceDiscovery {
567+
568+
private final ServiceEndpoint serviceEndpoint;
569+
570+
private NullServiceDiscovery(ServiceEndpoint serviceEndpoint) {
571+
this.serviceEndpoint = serviceEndpoint;
572+
}
573+
574+
@Override
575+
public Address address() {
576+
return Address.NULL_ADDRESS;
577+
}
578+
579+
@Override
580+
public ServiceEndpoint serviceEndpoint() {
581+
return serviceEndpoint;
582+
}
583+
584+
@Override
585+
public Flux<ServiceDiscoveryEvent> listenDiscovery() {
586+
return Flux.never();
587+
}
588+
589+
@Override
590+
public Mono<ServiceDiscovery> start() {
591+
return Mono.just(this);
592+
}
593+
594+
@Override
595+
public Mono<Void> shutdown() {
596+
return Mono.empty();
597+
}
598+
}
599+
564600
private static class GatewayBootstrap {
565601

566602
private final List<Function<GatewayOptions, Gateway>> factories = new ArrayList<>();
@@ -623,14 +659,13 @@ public static class ServiceTransportBootstrap {
623659

624660
public static final Supplier<ServiceTransport> NULL_SUPPLIER = () -> null;
625661
public static final ServiceTransportBootstrap NULL_INSTANCE = new ServiceTransportBootstrap();
626-
public static final Address NULL_ADDRESS = Address.create("0.0.0.0", 0);
627662

628663
private final Supplier<ServiceTransport> transportSupplier;
629664

630665
private ServiceTransport serviceTransport;
631666
private ClientTransport clientTransport;
632667
private ServerTransport serverTransport;
633-
private Address transportAddress = NULL_ADDRESS;
668+
private Address transportAddress = Address.NULL_ADDRESS;
634669

635670
public ServiceTransportBootstrap() {
636671
this(NULL_SUPPLIER);
@@ -643,7 +678,6 @@ public ServiceTransportBootstrap(Supplier<ServiceTransport> transportSupplier) {
643678
private Mono<ServiceTransportBootstrap> start(Microservices microservices) {
644679
if (transportSupplier == NULL_SUPPLIER
645680
|| (serviceTransport = transportSupplier.get()) == null) {
646-
LOGGER.info("[{}] ServiceTransport not set", microservices.id());
647681
return Mono.just(NULL_INSTANCE);
648682
}
649683

0 commit comments

Comments
 (0)