Search in sources :

Example 1 with SaturnThreadFactory

use of com.vip.saturn.job.threads.SaturnThreadFactory in project Saturn by vipshop.

the class TimeoutSchedulerExecutor method createScheduler.

public static synchronized ScheduledThreadPoolExecutor createScheduler(String executorName) {
    if (!scheduledThreadPoolExecutorMap.containsKey(executorName)) {
        ScheduledThreadPoolExecutor timeoutExecutor = new ScheduledThreadPoolExecutor(Math.max(2, Runtime.getRuntime().availableProcessors() / 2), new SaturnThreadFactory(executorName + "-timeout-watchdog", false));
        timeoutExecutor.setRemoveOnCancelPolicy(true);
        scheduledThreadPoolExecutorMap.put(executorName, timeoutExecutor);
        return timeoutExecutor;
    }
    return scheduledThreadPoolExecutorMap.get(executorName);
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) SaturnThreadFactory(com.vip.saturn.job.threads.SaturnThreadFactory)

Example 2 with SaturnThreadFactory

use of com.vip.saturn.job.threads.SaturnThreadFactory in project Saturn by vipshop.

the class ThreadA method main.

public static void main(String[] args) throws InterruptedException, ExecutionException {
    ScheduledThreadPoolExecutor timeoutExecutor = new ScheduledThreadPoolExecutor(Math.max(2, Runtime.getRuntime().availableProcessors() / 2), new SaturnThreadFactory("-timeout-watchdog"));
    timeoutExecutor.setRemoveOnCancelPolicy(true);
    ExecutorService executorService = Executors.newSingleThreadExecutor(new SaturnThreadFactory("test"));
    ThreadA a = new ThreadA();
    Future<String> future = executorService.submit(a);
    timeoutExecutor.schedule(new TimeoutHandleTask(a), 2, TimeUnit.SECONDS);
    String res = future.get();
    System.out.println("res:" + res);
    a = new ThreadA();
    future = executorService.submit(a);
    res = future.get();
    System.out.println("res:" + res);
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ExecutorService(java.util.concurrent.ExecutorService) SaturnThreadFactory(com.vip.saturn.job.threads.SaturnThreadFactory)

Example 3 with SaturnThreadFactory

use of com.vip.saturn.job.threads.SaturnThreadFactory in project Saturn by vipshop.

the class ShardingListenerManager method start.

@Override
public void start() {
    if (necessaryWatcher != null) {
        executorService = Executors.newSingleThreadExecutor(new SaturnThreadFactory(executorName + "-" + jobName + "-registerNecessaryWatcher", false));
        shardingService.registerNecessaryWatcher(necessaryWatcher);
        connectionStateListener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if ((newState == ConnectionState.CONNECTED) || (newState == ConnectionState.RECONNECTED)) {
                    // maybe node data have changed, so doBusiness whatever, it's okay for MSG job
                    LogUtils.info(log, jobName, "state change to {}, trigger doBusiness and register necessary watcher.", newState);
                    doBusiness();
                    registerNecessaryWatcher();
                }
            }
        };
        addConnectionStateListener(connectionStateListener);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ConnectionState(org.apache.curator.framework.state.ConnectionState) SaturnThreadFactory(com.vip.saturn.job.threads.SaturnThreadFactory) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener)

Example 4 with SaturnThreadFactory

use of com.vip.saturn.job.threads.SaturnThreadFactory in project Saturn by vipshop.

the class RestartAndDumpService method initDump.

private void initDump() throws Exception {
    dumpES = Executors.newSingleThreadExecutor(new SaturnThreadFactory(executorName + "-dump-watcher-thread", false));
    final String nodePath = SaturnExecutorsNode.EXECUTORS_ROOT + "/" + executorName + "/dump";
    coordinatorRegistryCenter.remove(nodePath);
    dumpNC = new NodeCache(curatorFramework, nodePath);
    dumpNC.getListenable().addListener(new NodeCacheListener() {

        @Override
        public void nodeChanged() throws Exception {
            // Watch create, update event
            if (dumpNC.getCurrentData() != null) {
                LogUtils.info(log, LogEvents.ExecutorEvent.DUMP, "The executor {} dump event is received", executorName);
                dumpES.execute(new Runnable() {

                    @Override
                    public void run() {
                        executeRestartOrDumpCmd("dump", LogEvents.ExecutorEvent.DUMP);
                        coordinatorRegistryCenter.remove(nodePath);
                    }
                });
            }
        }
    });
    dumpNC.start(false);
}
Also used : NodeCache(org.apache.curator.framework.recipes.cache.NodeCache) NodeCacheListener(org.apache.curator.framework.recipes.cache.NodeCacheListener) SaturnThreadFactory(com.vip.saturn.job.threads.SaturnThreadFactory) SaturnJobException(com.vip.saturn.job.exception.SaturnJobException)

Example 5 with SaturnThreadFactory

use of com.vip.saturn.job.threads.SaturnThreadFactory in project Saturn by vipshop.

the class SaturnExecutorService method registerCallbackAndStartExistingJob.

/**
 * Register NewJobCallback, and async start existing jobs. The TreeCache will publish the create events for already
 * existing jobs.
 */
public void registerCallbackAndStartExistingJob(final ScheduleNewJobCallback callback) throws Exception {
    jobsTreeCache = TreeCache.newBuilder((CuratorFramework) coordinatorRegistryCenter.getRawClient(), JobNodePath.ROOT).setExecutor(new CloseableExecutorService(Executors.newSingleThreadExecutor(new SaturnThreadFactory(executorName + "-$Jobs-watcher", false)), true)).setMaxDepth(1).build();
    jobsTreeCache.getListenable().addListener(new TreeCacheListener() {

        @Override
        public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
            if (event == null) {
                return;
            }
            ChildData data = event.getData();
            if (data == null) {
                return;
            }
            String path = data.getPath();
            if (path == null || path.equals(JobNodePath.ROOT)) {
                return;
            }
            Type type = event.getType();
            if (type == null || !type.equals(Type.NODE_ADDED)) {
                return;
            }
            String jobName = StringUtils.substringAfterLast(path, "/");
            String jobClassPath = JobNodePath.getNodeFullPath(jobName, ConfigurationNode.JOB_CLASS);
            // wait 5 seconds at most until jobClass created.
            for (int i = 0; i < WAIT_JOBCLASS_ADDED_COUNT; i++) {
                if (client.checkExists().forPath(jobClassPath) == null) {
                    Thread.sleep(200);
                    continue;
                }
                log.info("new job: {} 's jobClass created event received", jobName);
                if (!jobNames.contains(jobName)) {
                    if (callback.call(jobName)) {
                        jobNames.add(jobName);
                        log.info("the job {} initialize successfully", jobName);
                    } else {
                        log.warn("the job {} initialize fail", jobName);
                    }
                } else {
                    log.warn("the job {} is unnecessary to initialize, because it's already existing", jobName);
                }
                break;
            }
        }
    });
    jobsTreeCache.start();
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Type(org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) CloseableExecutorService(org.apache.curator.utils.CloseableExecutorService) ChildData(org.apache.curator.framework.recipes.cache.ChildData) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) SaturnThreadFactory(com.vip.saturn.job.threads.SaturnThreadFactory) TimeDiffIntolerableException(com.vip.saturn.job.exception.TimeDiffIntolerableException)

Aggregations

SaturnThreadFactory (com.vip.saturn.job.threads.SaturnThreadFactory)8 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)3 SaturnJobException (com.vip.saturn.job.exception.SaturnJobException)2 CuratorFramework (org.apache.curator.framework.CuratorFramework)2 NodeCache (org.apache.curator.framework.recipes.cache.NodeCache)2 NodeCacheListener (org.apache.curator.framework.recipes.cache.NodeCacheListener)2 TimeDiffIntolerableException (com.vip.saturn.job.exception.TimeDiffIntolerableException)1 ExtendableThreadPoolExecutor (com.vip.saturn.job.threads.ExtendableThreadPoolExecutor)1 TaskQueue (com.vip.saturn.job.threads.TaskQueue)1 ExecutorService (java.util.concurrent.ExecutorService)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 ChildData (org.apache.curator.framework.recipes.cache.ChildData)1 TreeCacheEvent (org.apache.curator.framework.recipes.cache.TreeCacheEvent)1 Type (org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type)1 TreeCacheListener (org.apache.curator.framework.recipes.cache.TreeCacheListener)1 ConnectionState (org.apache.curator.framework.state.ConnectionState)1 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)1 CloseableExecutorService (org.apache.curator.utils.CloseableExecutorService)1