You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vo...@apache.org on 2017/08/14 10:30:35 UTC

[25/50] ignite git commit: Lazy worker.

Lazy worker.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/40e45ad7
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/40e45ad7
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/40e45ad7

Branch: refs/heads/ignite-5991-6019
Commit: 40e45ad7663efdf59138239e2fd24a5f001a0e5b
Parents: 284387b
Author: devozerov <vo...@gridgain.com>
Authored: Mon Aug 14 10:15:31 2017 +0300
Committer: devozerov <vo...@gridgain.com>
Committed: Mon Aug 14 10:15:31 2017 +0300

----------------------------------------------------------------------
 .../h2/twostep/lazy/MapQueryLazyWorker.java     | 101 +++++++++++++++++++
 1 file changed, 101 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/40e45ad7/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/lazy/MapQueryLazyWorker.java
----------------------------------------------------------------------
diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/lazy/MapQueryLazyWorker.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/lazy/MapQueryLazyWorker.java
new file mode 100644
index 0000000..1cf9457
--- /dev/null
+++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/lazy/MapQueryLazyWorker.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.ignite.internal.processors.query.h2.twostep.lazy;
+
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.internal.IgniteInterruptedCheckedException;
+import org.apache.ignite.internal.util.worker.GridWorker;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingDeque;
+
+/**
+ * Worker for lazy query execution.
+ */
+public class MapQueryLazyWorker extends GridWorker {
+    /** Lazy thread flag. */
+    private static final ThreadLocal<MapQueryLazyWorker> LAZY_WORKER = new ThreadLocal<>();
+
+    /** Task to be executed. */
+    private final BlockingQueue<Runnable> tasks = new LinkedBlockingDeque<>();
+
+    /**
+     * Constructor.
+     *
+     * @param instanceName Instance name.
+     * @param name Worker name.
+     * @param log Logger.
+     */
+    public MapQueryLazyWorker(@Nullable String instanceName , String name, IgniteLogger log) {
+        super(instanceName, name, log);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void body() throws InterruptedException, IgniteInterruptedCheckedException {
+        LAZY_WORKER.set(this);
+
+        try {
+            while (!isCancelled()) {
+                Runnable task = tasks.poll();
+
+                if (task != null)
+                    task.run();
+            }
+        }
+        finally {
+            LAZY_WORKER.set(null);
+        }
+    }
+
+    /**
+     * Stop the worker.
+     */
+    public void stop() {
+        tasks.add(new StopTask());
+    }
+
+    /**
+     * Internal worker stop routine.
+     */
+    private void stop0() {
+        isCancelled = true;
+    }
+
+    /**
+     * @return Lazy worker flag.
+     */
+    private static boolean inLazyWorkerThread() {
+        MapQueryLazyWorker worker = LAZY_WORKER.get();
+
+        return worker != null;
+    }
+
+    /**
+     * Internal stop task.
+     */
+    private static class StopTask implements Runnable {
+        @Override public void run() {
+            MapQueryLazyWorker worker = LAZY_WORKER.get();
+
+            assert worker != null;
+
+            worker.stop0();
+        }
+    }
+}