public void start() throws CommandException { try { if (start.compareAndSet(false, true)) { port = config.getParameter("lts.command.port", 8719); commandExecutor = new ThreadPoolExecutor( Constants.AVAILABLE_PROCESSOR, Constants.AVAILABLE_PROCESSOR, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(100), new ThreadPoolExecutor.DiscardPolicy()); ServerSocket serverSocket = getServerSocket(); // 开启监听命令 startServerListener(serverSocket); LOGGER.info("Start CommandCenter succeed at port {}", port); } } catch (Exception t) { LOGGER.error( "Start CommandCenter error at port {} , use lts.command.port config change the port.", port, t); throw new CommandException(t); } }
/** * 发送JobResults * * @param results * @return */ private boolean retrySendJobResults(List<TaskTrackerJobResult> results) { // 发送消息给 JobTracker TtJobFinishedRequest requestBody = application.getCommandBodyWrapper().wrapper(new TtJobFinishedRequest()); requestBody.setTaskTrackerJobResults(results); requestBody.setReSend(true); int requestCode = JobProtos.RequestCode.JOB_FINISHED.code(); RemotingCommand request = RemotingCommand.createRequestCommand(requestCode, requestBody); try { // 这里一定要用同步,不然异步会发生文件锁,死锁 RemotingCommand commandResponse = remotingClient.invokeSync(request); if (commandResponse != null && commandResponse.getCode() == RemotingProtos.ResponseCode.SUCCESS.code()) { return true; } else { LOGGER.warn("Send job failed, {}", commandResponse); return false; } } catch (JobTrackerNotFoundException e) { LOGGER.error("Retry send job result failed! jobResults={}", results, e); } return false; }
public static void checkDuplicate(String path, boolean failOnError) { try { // 在ClassPath搜文件 Enumeration<URL> urls = ClassHelper.getCallerClassLoader(Version.class).getResources(path); Set<String> files = new HashSet<String>(); while (urls.hasMoreElements()) { URL url = urls.nextElement(); if (url != null) { String file = url.getFile(); if (file != null && file.length() > 0) { files.add(file); } } } // 如果有多个,就表示重复 if (files.size() > 1) { String error = "Duplicate class " + path + " in " + files.size() + " jar " + files; if (failOnError) { throw new IllegalStateException(error); } else { LOGGER.error(error); } } } catch (Throwable e) { // 防御性容错 LOGGER.error(e.getMessage(), e); } }
@Override public void destroy() { try { registry.destroy(); LOGGER.info("Destroy success!"); } catch (Throwable e) { LOGGER.error("Destroy failed!", e); } }
public void stop() { try { if (start.compareAndSet(true, false)) { commandExecutor.shutdownNow(); LOGGER.info("Stop CommandCenter succeed "); } } catch (Throwable t) { LOGGER.error("Stop CommandCenter error ", t); } }
private void checkPoint() throws IOException { if (LOGGER.isDebugEnabled()) { LOGGER.debug("checkpoint start"); } // 先将内容都强制刷到磁盘,因为后面会写头部汇总信息 if (fileLength != lastCheckPointLength) { fileChannel.force(true); lastCheckPointLength = fileLength; } if (LOGGER.isDebugEnabled()) { LOGGER.debug("checkpoint end: fileLength=" + fileLength); } }
public static String getVersion(Class<?> cls, String defaultVersion) { try { // 首先查找MANIFEST.MF规范中的版本号 String version = cls.getPackage().getImplementationVersion(); if (version == null || version.length() == 0) { version = cls.getPackage().getSpecificationVersion(); } if (version == null || version.length() == 0) { // 如果规范中没有版本号,基于jar包名获取版本号 CodeSource codeSource = cls.getProtectionDomain().getCodeSource(); if (codeSource == null) { LOGGER.info( "No codeSource for class " + cls.getName() + " when getVersion, use default version " + defaultVersion); } else { String file = codeSource.getLocation().getFile(); if (file != null && file.length() > 0 && file.endsWith(".jar")) { file = file.substring(0, file.length() - 4); int i = file.lastIndexOf('/'); if (i >= 0) { file = file.substring(i + 1); } i = file.indexOf("-"); if (i >= 0) { file = file.substring(i + 1); } while (file.length() > 0 && !Character.isDigit(file.charAt(0))) { i = file.indexOf("-"); if (i >= 0) { file = file.substring(i + 1); } else { break; } } version = file; } } } // 返回版本号,如果为空返回缺省版本号 return version == null || version.length() == 0 ? defaultVersion : version; } catch (Throwable e) { // 防御性容错 // 忽略异常,返回缺省版本号 LOGGER.error("return default version, ignore exception " + e.getMessage(), e); return defaultVersion; } }
public final void stop() { try { if (started.compareAndSet(true, false)) { registry.unregister(node); preRemotingStop(); remotingStop(); afterRemotingStop(); LOGGER.info("Stop success!"); } } catch (Throwable e) { LOGGER.error("Stop failed!", e); } }
/** * Log if the logger and the current event log level are compatible. We log a message and an * exception. * * @param eventLevel the event log level as requested by the user * @param message the message to log * @param cause the exception cause to log */ private void log(LogLevel eventLevel, String message, Throwable cause) { switch (eventLevel) { case TRACE: logger.trace(message, cause); return; case DEBUG: logger.debug(message, cause); return; case INFO: logger.info(message, cause); return; case WARN: logger.warn(message, cause); return; case ERROR: logger.error(message, cause); return; default: return; } }
public final void start() { try { if (started.compareAndSet(false, true)) { // 初始化配置 initConfig(); preRemotingStart(); remotingStart(); afterRemotingStart(); initRegistry(); registry.register(node); LOGGER.info("Start success!"); } } catch (Throwable e) { LOGGER.error("Start failed!", e); } }
@Override public Result run(Job job) throws Throwable { try { // Thread.sleep(1000L); // // if (job.getRetryTimes() > 5) { // return new Result(Action.EXECUTE_FAILED, "重试次数超过5次了,放过你吧!"); // } // // if (SystemClock.now() % 2 == 1) { // return new Result(Action.EXECUTE_LATER, "稍后执行"); // } // TODO 业务逻辑 LOGGER.info("我要执行:" + job); // 会发送到 LTS (JobTracker上) bizLogger.info("测试,业务日志啊啊啊啊啊"); } catch (Exception e) { LOGGER.info("Run job failed!", e); return new Result(Action.EXECUTE_FAILED, e.getMessage()); } return new Result(Action.EXECUTE_SUCCESS, "执行成功了,哈哈"); }
protected void initConfig() { application.setEventCenter(eventCenterFactory.getEventCenter(config)); application.setCommandBodyWrapper(new CommandBodyWrapper(config)); application.setMasterElector(new MasterElector(application)); application.getMasterElector().addMasterChangeListener(masterChangeListeners); application.setRegistryStatMonitor(new RegistryStatMonitor(application)); node = NodeFactory.create(getNodeClass(), config); config.setNodeType(node.getNodeType()); LOGGER.info("Current node config :{}", config); // 订阅的node管理 SubscribedNodeManager subscribedNodeManager = new SubscribedNodeManager(application); application.setSubscribedNodeManager(subscribedNodeManager); nodeChangeListeners.add(subscribedNodeManager); // 用于master选举的监听器 nodeChangeListeners.add(new MasterElectionListener(application)); // 监听自己节点变化(如,当前节点被禁用了) nodeChangeListeners.add(new SelfChangeListener(application)); }
public static void setJSONAdapter(JSONAdapter jsonAdapter) { if (jsonAdapter != null) { LOGGER.info("Using JSON lib " + jsonAdapter.getName()); JSONFactory.JSON_ADAPTER = jsonAdapter; } }