Skip to content

Commit

Permalink
Add cluster endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo committed Nov 9, 2017
1 parent a77b8b4 commit 6fb1c77
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 21 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/io/atomix/Atomix.java
Expand Up @@ -400,7 +400,7 @@ public Atomix build() {
ManagedClusterCommunicationService clusterCommunicator = buildClusterCommunicationService(clusterService, messagingService);
ManagedPartitionService partitionService = buildPartitionService(clusterCommunicator);
PrimitiveService primitives = buildPrimitiveService(partitionService);
ManagedRestService restService = buildRestService(primitives);
ManagedRestService restService = buildRestService(clusterService, primitives);
return new Atomix(
clusterService,
messagingService,
Expand Down Expand Up @@ -461,8 +461,8 @@ private PrimitiveService buildPrimitiveService(PartitionService partitionService
/**
* Builds a REST service.
*/
private ManagedRestService buildRestService(PrimitiveService primitiveService) {
return httpPort > 0 ? new VertxRestService(localNode.endpoint().host().getHostAddress(), httpPort, primitiveService) : null;
private ManagedRestService buildRestService(ClusterService clusterService, PrimitiveService primitiveService) {
return httpPort > 0 ? new VertxRestService(localNode.endpoint().host().getHostAddress(), httpPort, clusterService, primitiveService) : null;
}

/**
Expand Down
44 changes: 44 additions & 0 deletions rest/src/main/java/io/atomix/rest/impl/AtomixResource.java
@@ -0,0 +1,44 @@
/*
* Copyright 2017-present Open Networking Foundation
*
* 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 io.atomix.rest.impl;

import io.atomix.cluster.ClusterService;

import javax.ws.rs.Path;

/**
* Atomix resource.
*/
@Path("/")
public class AtomixResource {
private final ClusterService clusterService;
private final PrimitiveCache primitiveCache;

public AtomixResource(ClusterService clusterService, PrimitiveCache primitiveCache) {
this.clusterService = clusterService;
this.primitiveCache = primitiveCache;
}

@Path("/cluster")
public ClusterResource getCluster() {
return new ClusterResource(clusterService);
}

@Path("/primitives")
public PrimitivesResource getPrimitives() {
return new PrimitivesResource(primitiveCache);
}
}
Expand Up @@ -15,44 +15,41 @@
*/
package io.atomix.rest.impl;

import io.atomix.cluster.ClusterService;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.HttpResponse;
import org.jboss.resteasy.spi.ResourceFactory;
import org.jboss.resteasy.spi.ResteasyProviderFactory;

import java.util.function.Function;

/**
* Vert.x REST resource factory.
* Atomix resource factory.
*/
public class VertxRestResourceFactory implements ResourceFactory {
private final Class<?> resourceClass;
private final Function<PrimitiveCache, AbstractRestResource> resourceFactory;
public class AtomixResourceFactory implements ResourceFactory {
private final ClusterService clusterService;
private final PrimitiveCache primitiveCache;

public VertxRestResourceFactory(Class<?> resourceClass, Function<PrimitiveCache, AbstractRestResource> resourceFactory, PrimitiveCache primitiveCache) {
this.resourceClass = resourceClass;
this.resourceFactory = resourceFactory;
public AtomixResourceFactory(ClusterService clusterService, PrimitiveCache primitiveCache) {
this.clusterService = clusterService;
this.primitiveCache = primitiveCache;
}

@Override
public Class<?> getScannableClass() {
return resourceClass;
return AtomixResource.class;
}

@Override
public void registered(ResteasyProviderFactory factory) {
public void registered(ResteasyProviderFactory resteasyProviderFactory) {

}

@Override
public Object createResource(HttpRequest request, HttpResponse response, ResteasyProviderFactory factory) {
return resourceFactory.apply(primitiveCache);
public Object createResource(HttpRequest httpRequest, HttpResponse httpResponse, ResteasyProviderFactory resteasyProviderFactory) {
return new AtomixResource(clusterService, primitiveCache);
}

@Override
public void requestFinished(HttpRequest request, HttpResponse response, Object resource) {
public void requestFinished(HttpRequest httpRequest, HttpResponse httpResponse, Object o) {

}

Expand Down
96 changes: 96 additions & 0 deletions rest/src/main/java/io/atomix/rest/impl/ClusterResource.java
@@ -0,0 +1,96 @@
/*
* Copyright 2017-present Open Networking Foundation
*
* 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 io.atomix.rest.impl;

import io.atomix.cluster.ClusterService;
import io.atomix.cluster.Node;
import io.atomix.cluster.NodeId;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import java.util.stream.Collectors;

/**
* Cluster resource.
*/
public class ClusterResource extends AbstractRestResource {
private final ClusterService clusterService;

public ClusterResource(ClusterService clusterService) {
this.clusterService = clusterService;
}

@GET
@Path("/node")
@Produces(MediaType.APPLICATION_JSON)
public Response getNode() {
return Response.ok(new NodeInfo(clusterService.localNode())).build();
}

@GET
@Path("/nodes")
@Produces(MediaType.APPLICATION_JSON)
public Response getNodes() {
return Response.ok(clusterService.nodes().stream().map(NodeInfo::new).collect(Collectors.toList())).build();
}

@GET
@Path("/nodes/{node}")
@Produces(MediaType.APPLICATION_JSON)
public Response getNodeInfo(@PathParam("node") String nodeId) {
Node node = clusterService.node(NodeId.from(nodeId));
if (node == null) {
return Response.status(Status.NOT_FOUND).build();
}
return Response.ok(new NodeInfo(node)).build();
}

/**
* Node information.
*/
static class NodeInfo {
private final Node node;

NodeInfo(Node node) {
this.node = node;
}

public String getId() {
return node.id().id();
}

public String getHost() {
return node.endpoint().host().getHostAddress();
}

public int getPort() {
return node.endpoint().port();
}

public Node.Type getType() {
return node.type();
}

public Node.State getStatus() {
return node.state();
}
}
}
Expand Up @@ -21,7 +21,6 @@
/**
* Primitives resource.
*/
@Path("/primitives")
public class PrimitivesResource extends AbstractRestResource {
private final PrimitiveCache primitiveCache;

Expand Down
9 changes: 7 additions & 2 deletions rest/src/main/java/io/atomix/rest/impl/VertxRestService.java
Expand Up @@ -15,6 +15,7 @@
*/
package io.atomix.rest.impl;

import io.atomix.cluster.ClusterService;
import io.atomix.primitives.PrimitiveService;
import io.atomix.rest.ManagedRestService;
import io.atomix.rest.RestService;
Expand All @@ -28,6 +29,8 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* Vert.x REST service.
*/
Expand All @@ -38,15 +41,17 @@ public class VertxRestService implements ManagedRestService {
private final String host;
private final int port;
private final Vertx vertx;
private final ClusterService clusterService;
private final PrimitiveCache primitiveCache;
private HttpServer server;
private VertxResteasyDeployment deployment;
private final AtomicBoolean open = new AtomicBoolean();

public VertxRestService(String host, int port, PrimitiveService primitiveService) {
public VertxRestService(String host, int port, ClusterService clusterService, PrimitiveService primitiveService) {
this.host = host;
this.port = port;
this.vertx = Vertx.vertx();
this.clusterService = checkNotNull(clusterService);
this.primitiveCache = new PrimitiveCache(primitiveService, PRIMITIVE_CACHE_SIZE);
}

Expand All @@ -55,7 +60,7 @@ public CompletableFuture<RestService> open() {
server = vertx.createHttpServer();
deployment = new VertxResteasyDeployment();
deployment.start();
deployment.getRegistry().addResourceFactory(new VertxRestResourceFactory(PrimitivesResource.class, PrimitivesResource::new, primitiveCache));
deployment.getRegistry().addResourceFactory(new AtomixResourceFactory(clusterService, primitiveCache));
server.requestHandler(new VertxRequestHandler(vertx, deployment));

CompletableFuture<RestService> future = new CompletableFuture<>();
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/resources/logback.xml
Expand Up @@ -21,7 +21,7 @@
</encoder>
</appender>

<logger name="io.atomix" level="TRACE" />
<logger name="io.atomix" level="INFO" />
<logger name="io.netty.handler.logging.LoggingHandler" level="INFO"/>

<root level="INFO">
Expand Down

0 comments on commit 6fb1c77

Please sign in to comment.