|
| 1 | +package com.cloudata.blockstore; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.net.InetSocketAddress; |
| 5 | +import java.net.SocketAddress; |
| 6 | +import java.util.EnumSet; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import javax.servlet.DispatcherType; |
| 10 | + |
| 11 | +import org.eclipse.jetty.server.Server; |
| 12 | +import org.eclipse.jetty.servlet.FilterHolder; |
| 13 | +import org.eclipse.jetty.servlet.ServletContextHandler; |
| 14 | +import org.robotninjas.barge.ClusterConfig; |
| 15 | +import org.robotninjas.barge.RaftService; |
| 16 | +import org.robotninjas.barge.Replica; |
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
| 19 | + |
| 20 | +import com.cloudata.blockstore.iscsi.IscsiEndpoint; |
| 21 | +import com.cloudata.blockstore.iscsi.IscsiServer; |
| 22 | +import com.cloudata.blockstore.web.WebModule; |
| 23 | +import com.google.common.base.Throwables; |
| 24 | +import com.google.common.collect.Lists; |
| 25 | +import com.google.inject.Guice; |
| 26 | +import com.google.inject.Injector; |
| 27 | +import com.google.inject.servlet.GuiceFilter; |
| 28 | + |
| 29 | +public class BlockStoreServer { |
| 30 | + private static final Logger log = LoggerFactory.getLogger(BlockStoreServer.class); |
| 31 | + |
| 32 | + final File baseDir; |
| 33 | + final int httpPort; |
| 34 | + private final Replica local; |
| 35 | + private final List<Replica> peers; |
| 36 | + private RaftService raft; |
| 37 | + private final SocketAddress iscsiSocketAddress; |
| 38 | + private IscsiEndpoint redisEndpoint; |
| 39 | + private Server jetty; |
| 40 | + |
| 41 | + public BlockStoreServer(File baseDir, Replica local, List<Replica> peers, int httpPort, |
| 42 | + SocketAddress iscsiSocketAddress) { |
| 43 | + this.baseDir = baseDir; |
| 44 | + this.local = local; |
| 45 | + this.peers = peers; |
| 46 | + this.httpPort = httpPort; |
| 47 | + this.iscsiSocketAddress = iscsiSocketAddress; |
| 48 | + } |
| 49 | + |
| 50 | + public synchronized void start() throws Exception { |
| 51 | + if (raft != null || jetty != null) { |
| 52 | + throw new IllegalStateException(); |
| 53 | + } |
| 54 | + |
| 55 | + File logDir = new File(baseDir, "logs"); |
| 56 | + File stateDir = new File(baseDir, "state"); |
| 57 | + |
| 58 | + logDir.mkdirs(); |
| 59 | + stateDir.mkdirs(); |
| 60 | + |
| 61 | + KeyValueStateMachine stateMachine = new KeyValueStateMachine(); |
| 62 | + |
| 63 | + ClusterConfig config = ClusterConfig.from(local, peers); |
| 64 | + this.raft = RaftService.newBuilder(config).logDir(logDir).timeout(300).build(stateMachine); |
| 65 | + |
| 66 | + stateMachine.init(raft, stateDir); |
| 67 | + |
| 68 | + raft.startAsync().awaitRunning(); |
| 69 | + |
| 70 | + // final String baseUri = getHttpUrl(); |
| 71 | + |
| 72 | + Injector injector = Guice.createInjector(new KeyValueModule(stateMachine), new WebModule()); |
| 73 | + |
| 74 | + // ResourceConfig rc = new PackagesResourceConfig(WebModule.class.getPackage().getName()); |
| 75 | + // IoCComponentProviderFactory ioc = new GuiceComponentProviderFactory(rc, injector); |
| 76 | + |
| 77 | + // this.selector = GrizzlyServerFactory.create(baseUri, rc, ioc); |
| 78 | + |
| 79 | + this.jetty = new Server(httpPort); |
| 80 | + |
| 81 | + ServletContextHandler context = new ServletContextHandler(); |
| 82 | + context.setContextPath("/"); |
| 83 | + |
| 84 | + FilterHolder filterHolder = new FilterHolder(injector.getInstance(GuiceFilter.class)); |
| 85 | + context.addFilter(filterHolder, "*", EnumSet.of(DispatcherType.REQUEST)); |
| 86 | + |
| 87 | + jetty.setHandler(context); |
| 88 | + |
| 89 | + jetty.start(); |
| 90 | + |
| 91 | + if (iscsiSocketAddress != null) { |
| 92 | + long storeId = 1; |
| 93 | + IscsiServer iscsiServer = new IscsiServer(stateMachine, storeId); |
| 94 | + |
| 95 | + this.redisEndpoint = new IscsiEndpoint(iscsiSocketAddress, iscsiServer); |
| 96 | + this.redisEndpoint.start(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public String getHttpUrl() { |
| 101 | + return "http://localhost:" + httpPort + "/"; |
| 102 | + } |
| 103 | + |
| 104 | + public static void main(String... args) throws Exception { |
| 105 | + final int port = Integer.parseInt(args[0]); |
| 106 | + |
| 107 | + Replica local = Replica.fromString("localhost:" + (10000 + port)); |
| 108 | + List<Replica> members = Lists.newArrayList(Replica.fromString("localhost:10001")); |
| 109 | + // Replica.fromString("localhost:10002"), Replica.fromString("localhost:10003")); |
| 110 | + members.remove(local); |
| 111 | + |
| 112 | + File baseDir = new File(args[0]); |
| 113 | + int httpPort = (9990 + port); |
| 114 | + int iscsiPort = 3260 + port; |
| 115 | + |
| 116 | + SocketAddress iscsiSocketAddress = new InetSocketAddress(iscsiPort); |
| 117 | + final BlockStoreServer server = new BlockStoreServer(baseDir, local, members, httpPort, iscsiSocketAddress); |
| 118 | + server.start(); |
| 119 | + |
| 120 | + Runtime.getRuntime().addShutdownHook(new Thread() { |
| 121 | + @Override |
| 122 | + public void run() { |
| 123 | + try { |
| 124 | + server.stop(); |
| 125 | + } catch (Exception e) { |
| 126 | + log.error("Error stopping server", e); |
| 127 | + } |
| 128 | + } |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + public synchronized void stop() throws Exception { |
| 133 | + if (jetty != null) { |
| 134 | + jetty.stop(); |
| 135 | + jetty = null; |
| 136 | + } |
| 137 | + |
| 138 | + if (redisEndpoint != null) { |
| 139 | + try { |
| 140 | + redisEndpoint.stop(); |
| 141 | + } catch (InterruptedException e) { |
| 142 | + Thread.currentThread().interrupt(); |
| 143 | + throw Throwables.propagate(e); |
| 144 | + } |
| 145 | + redisEndpoint = null; |
| 146 | + } |
| 147 | + |
| 148 | + if (raft != null) { |
| 149 | + raft.stopAsync().awaitTerminated(); |
| 150 | + raft = null; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + public SocketAddress getRedisSocketAddress() { |
| 155 | + return iscsiSocketAddress; |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments