use of com.bonree.brfs.common.service.impl.DefaultServiceManager in project BRFS by zhangnianli.
the class TestServiceManager method main.
public static void main(String[] args) throws Exception {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient(zk_address, 5 * 1000, 30 * 1000, retryPolicy);
client.start();
client.blockUntilConnected();
ServiceManager sm = new DefaultServiceManager(client.usingNamespace("test_c"));
sm.start();
sm.addServiceStateListener("ss_g", new ServiceStateListener() {
@Override
public void serviceRemoved(Service service) {
// TODO Auto-generated method stub
}
@Override
public void serviceAdded(Service service) {
System.out.println("##########################################before add " + service);
}
});
sm.registerService(new Service("ss_1", "ss_g", "localhost", 999));
Thread.sleep(1000);
sm.addServiceStateListener("ss_g", new ServiceStateListener() {
@Override
public void serviceRemoved(Service service) {
// TODO Auto-generated method stub
}
@Override
public void serviceAdded(Service service) {
System.out.println("##########################################after add " + service);
}
});
sm.addServiceStateListener("ss_g", new ServiceStateListener() {
@Override
public void serviceRemoved(Service service) {
// TODO Auto-generated method stub
}
@Override
public void serviceAdded(Service service) {
System.out.println("##########################################after_2 add " + service);
}
});
sm.registerService(new Service("ss_2", "ss_g", "localhost", 1222));
synchronized (sm) {
sm.wait();
}
}
use of com.bonree.brfs.common.service.impl.DefaultServiceManager in project BRFS by zhangnianli.
the class TestService method main.
public static void main(String[] args) throws Exception {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.101.86:2181", 3000, 3000, retryPolicy);
client.start();
client.blockUntilConnected();
ServiceManager sm = new DefaultServiceManager(client.usingNamespace("test"));
sm.start();
Service s = new Service();
s.setServiceGroup("group");
s.setServiceId("ser_" + ID);
s.setHost("local");
sm.registerService(s);
Service tmp = sm.getServiceById(s.getServiceGroup(), s.getServiceId());
System.out.println(tmp);
sm.addServiceStateListener("group", new ServiceStateListener() {
@Override
public void serviceRemoved(Service service) {
System.out.println("remove--" + service.getServiceId());
}
@Override
public void serviceAdded(Service service) {
System.out.println("add--" + service.getServiceId());
}
});
System.out.println(sm.getServiceById(s.getServiceGroup(), s.getServiceId()));
}
use of com.bonree.brfs.common.service.impl.DefaultServiceManager in project BRFS by zhangnianli.
the class ServerMain method main.
public static void main(String[] args) {
ProcessFinalizer finalizer = new ProcessFinalizer();
try {
ResourceTaskConfig resourceConfig = ResourceTaskConfig.parse();
// 初始化email发送配置
EmailPool.getInstance();
String zkAddresses = Configs.getConfiguration().GetConfig(CommonConfigs.CONFIG_ZOOKEEPER_ADDRESSES);
CuratorClient leaderClient = CuratorClient.getClientInstance(zkAddresses, 1000, 1000);
CuratorClient client = CuratorClient.getClientInstance(zkAddresses);
CuratorCacheFactory.init(zkAddresses);
ZookeeperPaths zookeeperPaths = ZookeeperPaths.create(Configs.getConfiguration().GetConfig(CommonConfigs.CONFIG_CLUSTER_NAME), zkAddresses);
SimpleAuthentication authentication = SimpleAuthentication.getAuthInstance(zookeeperPaths.getBaseUserPath(), zookeeperPaths.getBaseLocksPath(), client.getInnerClient());
UserModel model = authentication.getUser("root");
if (model == null) {
LOG.error("please init server!!!");
System.exit(1);
}
ServerIDManager idManager = new ServerIDManager(client.getInnerClient(), zookeeperPaths);
idManager.getFirstServerID();
StorageRegionManager snManager = new DefaultStorageRegionManager(client.getInnerClient().usingNamespace(zookeeperPaths.getBaseClusterName().substring(1)), null);
snManager.addStorageRegionStateListener(new StorageRegionStateListener() {
@Override
public void storageRegionAdded(StorageRegion node) {
LOG.info("-----------StorageNameAdded--[{}]", node);
idManager.getSecondServerID(node.getId());
}
@Override
public void storageRegionUpdated(StorageRegion node) {
}
@Override
public void storageRegionRemoved(StorageRegion node) {
LOG.info("-----------StorageNameRemove--[{}]", node);
idManager.deleteSecondServerID(node.getId());
}
});
snManager.start();
finalizer.add(snManager);
ServiceManager sm = new DefaultServiceManager(client.getInnerClient().usingNamespace(zookeeperPaths.getBaseClusterName().substring(1)));
sm.start();
finalizer.add(sm);
// 磁盘管理模块
// EmptyMain diskMain = new EmptyMain(sm);
// diskMain.start();
//
// finalizer.add(diskMain);
DataNodeBootStrap dataNodeBootStrap = new DataNodeBootStrap(sm);
dataNodeBootStrap.start();
finalizer.add(dataNodeBootStrap);
// 副本平衡模块
sm.addServiceStateListener(Configs.getConfiguration().GetConfig(CommonConfigs.CONFIG_DATA_SERVICE_GROUP_NAME), new ServerChangeTaskGenetor(leaderClient, client, sm, idManager, zookeeperPaths.getBaseRebalancePath(), 3000, snManager));
@SuppressWarnings("resource") RebalanceManager rebalanceServer = new RebalanceManager(zookeeperPaths, idManager, snManager, sm);
rebalanceServer.start();
String host = Configs.getConfiguration().GetConfig(DataNodeConfigs.CONFIG_HOST);
int port = Configs.getConfiguration().GetConfig(DataNodeConfigs.CONFIG_PORT);
int readPort = Configs.getConfiguration().GetConfig(DataNodeConfigs.CONFIG_FILE_PORT);
Service selfService = new Service();
selfService.setHost(host);
selfService.setPort(port);
selfService.setExtraPort(readPort);
selfService.setServiceGroup(Configs.getConfiguration().GetConfig(CommonConfigs.CONFIG_DATA_SERVICE_GROUP_NAME));
String serviceId = idManager.getFirstServerID();
selfService.setServiceId(serviceId);
Service checkService = sm.getServiceById(Configs.getConfiguration().GetConfig(CommonConfigs.CONFIG_DATA_SERVICE_GROUP_NAME), serviceId);
if (checkService == null) {
sm.registerService(selfService);
System.out.println(selfService);
} else {
LOG.error("serviceId : {} is exists, system will exit!!!", serviceId);
System.exit(1);
}
finalizer.add(new Closeable() {
@Override
public void close() throws IOException {
try {
sm.unregisterService(selfService);
} catch (Exception e) {
LOG.error("unregister service[{}] error", selfService, e);
}
}
});
// 资源管理模块
InitTaskManager.initManager(resourceConfig, zookeeperPaths, sm, snManager, idManager, client);
} catch (Exception e) {
LOG.error("launch server error!!!", e);
System.exit(1);
} finally {
Runtime.getRuntime().addShutdownHook(finalizer);
}
}
use of com.bonree.brfs.common.service.impl.DefaultServiceManager in project BRFS by zhangnianli.
the class BootStrap method main.
public static void main(String[] args) {
ProcessFinalizer finalizer = new ProcessFinalizer();
try {
// 初始化email发送配置
EmailPool.getInstance();
String zkAddresses = Configs.getConfiguration().GetConfig(CommonConfigs.CONFIG_ZOOKEEPER_ADDRESSES);
String host = Configs.getConfiguration().GetConfig(RegionNodeConfigs.CONFIG_HOST);
int port = Configs.getConfiguration().GetConfig(RegionNodeConfigs.CONFIG_PORT);
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient(zkAddresses, 3000, 15000, retryPolicy);
client.start();
client.blockUntilConnected();
finalizer.add(client);
CuratorCacheFactory.init(zkAddresses);
String clusterName = Configs.getConfiguration().GetConfig(CommonConfigs.CONFIG_CLUSTER_NAME);
ZookeeperPaths zookeeperPaths = ZookeeperPaths.create(clusterName, zkAddresses);
ServerIDManager idManager = new ServerIDManager(client, zookeeperPaths);
SimpleAuthentication simpleAuthentication = SimpleAuthentication.getAuthInstance(zookeeperPaths.getBaseUserPath(), zookeeperPaths.getBaseLocksPath(), client);
UserModel model = simpleAuthentication.getUser("root");
if (model == null) {
LOG.error("please init server!!!");
System.exit(1);
}
Service service = new Service(UUID.randomUUID().toString(), Configs.getConfiguration().GetConfig(CommonConfigs.CONFIG_REGION_SERVICE_GROUP_NAME), host, port);
ServiceManager serviceManager = new DefaultServiceManager(client.usingNamespace(zookeeperPaths.getBaseClusterName().substring(1)));
serviceManager.start();
finalizer.add(serviceManager);
TimeExchangeEventEmitter timeEventEmitter = new TimeExchangeEventEmitter(2);
finalizer.add(timeEventEmitter);
StorageRegionIdBuilder storageIdBuilder = new ZkStorageRegionIdBuilder(client.usingNamespace(zookeeperPaths.getBaseClusterName().substring(1)));
StorageRegionManager storageNameManager = new DefaultStorageRegionManager(client.usingNamespace(zookeeperPaths.getBaseClusterName().substring(1)), storageIdBuilder);
storageNameManager.start();
finalizer.add(storageNameManager);
// HttpDiskNodeConnectionPool connectionPool = new HttpDiskNodeConnectionPool(serviceManager);
// finalizer.add(connectionPool);
AsyncTcpClientGroup tcpClientGroup = new AsyncTcpClientGroup(Configs.getConfiguration().GetConfig(RegionNodeConfigs.CONFIG_WRITER_WORKER_NUM));
TcpDiskNodeConnectionPool connectionPool = new TcpDiskNodeConnectionPool(serviceManager, tcpClientGroup);
finalizer.add(tcpClientGroup);
FilePathMaker pathMaker = new IDFilePathMaker(idManager);
int workerThreadNum = Configs.getConfiguration().GetConfig(RegionNodeConfigs.CONFIG_SERVER_IO_THREAD_NUM);
HttpConfig httpConfig = HttpConfig.newBuilder().setHost(host).setPort(port).setAcceptWorkerNum(1).setRequestHandleWorkerNum(workerThreadNum).setBacklog(Integer.parseInt(System.getProperty(SystemProperties.PROP_NET_BACKLOG, "2048"))).build();
NettyHttpServer httpServer = new NettyHttpServer(httpConfig);
httpServer.addHttpAuthenticator(new HttpAuthenticator() {
@Override
public int check(String userName, String passwd) {
StringBuilder tokenBuilder = new StringBuilder();
tokenBuilder.append(userName).append(":").append(passwd);
return simpleAuthentication.auth(tokenBuilder.toString()) ? 0 : ReturnCode.USER_FORBID.getCode();
}
});
ExecutorService requestHandlerExecutor = Executors.newFixedThreadPool(Math.max(4, Runtime.getRuntime().availableProcessors() / 4), new PooledThreadFactory("request_handler"));
finalizer.add(new Closeable() {
@Override
public void close() throws IOException {
requestHandlerExecutor.shutdown();
}
});
FileObjectSyncProcessor processor = new DefaultFileObjectSyncProcessor(connectionPool, pathMaker);
DefaultFileObjectSynchronier fileSynchronizer = new DefaultFileObjectSynchronier(processor, serviceManager, 10, TimeUnit.SECONDS);
fileSynchronizer.start();
finalizer.add(fileSynchronizer);
FileNodeStorer storer = new ZkFileNodeStorer(client.usingNamespace(zookeeperPaths.getBaseClusterName().substring(1)), ZkFileCoordinatorPaths.COORDINATOR_FILESTORE);
// 资源缓存器
String diskGroup = Configs.getConfiguration().GetConfig(CommonConfigs.CONFIG_DATA_SERVICE_GROUP_NAME);
String rPath = zookeeperPaths.getBaseResourcesPath() + "/" + diskGroup + "/resource";
ClusterResource clusterResource = ClusterResource.newBuilder().setCache(true).setClient(client).setListenPath(rPath).setPool(Executors.newSingleThreadExecutor()).build().start();
// 资源选择策略
// 获取限制值
double diskRemainRate = Configs.getConfiguration().GetConfig(ResourceConfigs.CONFIG_LIMIT_DISK_AVAILABLE_RATE);
double diskForceRemainRate = Configs.getConfiguration().GetConfig(ResourceConfigs.CONFIG_LIMIT_FORCE_DISK_AVAILABLE_RATE);
double diskwriteValue = Configs.getConfiguration().GetConfig(ResourceConfigs.CONFIG_LIMIT_DISK_WRITE_SPEED);
double diskForcewriteValue = Configs.getConfiguration().GetConfig(ResourceConfigs.CONFIG_LIMIT_FORCE_DISK_WRITE_SPEED);
long diskRemainSize = Configs.getConfiguration().GetConfig(ResourceConfigs.CONFIG_LIMIT_DISK_REMAIN_SIZE);
long diskForceRemainSize = Configs.getConfiguration().GetConfig(ResourceConfigs.CONFIG_LIMIT_FORCE_DISK_REMAIN_SIZE);
LimitServerResource lmit = new LimitServerResource();
lmit.setDiskRemainRate(diskRemainRate);
lmit.setDiskWriteValue(diskwriteValue);
lmit.setForceDiskRemainRate(diskForceRemainRate);
lmit.setForceWriteValue(diskForcewriteValue);
lmit.setRemainWarnSize(diskRemainSize);
lmit.setRemainForceSize(diskForceRemainSize);
int centSize = Configs.getConfiguration().GetConfig(ResourceConfigs.CONFIG_RESOURCE_CENT_SIZE);
long fileSize = Configs.getConfiguration().GetConfig(DataNodeConfigs.CONFIG_FILE_MAX_CAPACITY) / 1024;
MachineResourceWriterSelector serviceSelector = new MachineResourceWriterSelector(connectionPool, storer, lmit, diskGroup, fileSize, centSize);
// 生成备用选择器
DuplicateNodeSelector bakSelect = new MinimalDuplicateNodeSelector(serviceManager, connectionPool);
// 选择
DuplicateNodeSelector nodeSelector = ResourceWriteSelector.newBuilder().setBakSelector(bakSelect).setDaemon(clusterResource).setGroupName(diskGroup).setStorageRegionManager(storageNameManager).setResourceSelector(serviceSelector).build();
FileObjectFactory fileFactory = new DefaultFileObjectFactory(service, storer, nodeSelector, idManager, connectionPool);
int closerThreadNum = Configs.getConfiguration().GetConfig(RegionNodeConfigs.CONFIG_CLOSER_THREAD_NUM);
DefaultFileObjectCloser fileCloser = new DefaultFileObjectCloser(closerThreadNum, fileSynchronizer, storer, connectionPool, pathMaker);
finalizer.add(fileCloser);
FileNodeSinkManager sinkManager = new ZkFileNodeSinkManager(client.usingNamespace(zookeeperPaths.getBaseClusterName().substring(1)), service, serviceManager, timeEventEmitter, storer, new RandomFileNodeSinkSelector(), fileCloser);
sinkManager.start();
finalizer.add(sinkManager);
DataPoolFactory dataPoolFactory = new BlockingQueueDataPoolFactory(Configs.getConfiguration().GetConfig(RegionNodeConfigs.CONFIG_DATA_POOL_CAPACITY));
FileObjectSupplierFactory fileSupplierFactory = new DefaultFileObjectSupplierFactory(fileFactory, fileCloser, fileSynchronizer, sinkManager, timeEventEmitter);
DiskWriter diskWriter = new DiskWriter(Configs.getConfiguration().GetConfig(RegionNodeConfigs.CONFIG_WRITER_WORKER_NUM), connectionPool, pathMaker);
finalizer.add(diskWriter);
DataEngineFactory engineFactory = new DefaultDataEngineFactory(dataPoolFactory, fileSupplierFactory, diskWriter);
DefaultDataEngineManager engineManager = new DefaultDataEngineManager(storageNameManager, engineFactory);
finalizer.add(engineManager);
DefaultStorageRegionWriter writer = new DefaultStorageRegionWriter(engineManager);
NettyHttpRequestHandler requestHandler = new NettyHttpRequestHandler(requestHandlerExecutor);
requestHandler.addMessageHandler("POST", new WriteDataMessageHandler(writer));
requestHandler.addMessageHandler("GET", new ReadDataMessageHandler());
requestHandler.addMessageHandler("DELETE", new DeleteDataMessageHandler(zookeeperPaths, serviceManager, storageNameManager));
httpServer.addContextHandler(URI_DATA_ROOT, requestHandler);
NettyHttpRequestHandler snRequestHandler = new NettyHttpRequestHandler(requestHandlerExecutor);
snRequestHandler.addMessageHandler("PUT", new CreateStorageRegionMessageHandler(storageNameManager));
snRequestHandler.addMessageHandler("POST", new UpdateStorageRegionMessageHandler(storageNameManager));
snRequestHandler.addMessageHandler("GET", new OpenStorageRegionMessageHandler(storageNameManager));
snRequestHandler.addMessageHandler("DELETE", new DeleteStorageRegionMessageHandler(zookeeperPaths, storageNameManager, serviceManager));
httpServer.addContextHandler(URI_STORAGE_REGION_ROOT, snRequestHandler);
httpServer.start();
finalizer.add(httpServer);
serviceManager.registerService(service);
finalizer.add(new Closeable() {
@Override
public void close() throws IOException {
try {
serviceManager.unregisterService(service);
} catch (Exception e) {
LOG.error("unregister service[{}] error", service, e);
}
}
});
} catch (Exception e) {
LOG.error("launch server error!!!", e);
System.exit(1);
} finally {
Runtime.getRuntime().addShutdownHook(finalizer);
}
}
Aggregations