Search in sources :

Example 91 with AtomicBoolean

use of java.util.concurrent.atomic.AtomicBoolean in project hive by apache.

the class TestCompactor method runWorker.

static void runWorker(HiveConf hiveConf) throws MetaException {
    AtomicBoolean stop = new AtomicBoolean(true);
    Worker t = new Worker();
    t.setThreadId((int) t.getId());
    t.setHiveConf(hiveConf);
    AtomicBoolean looped = new AtomicBoolean();
    t.init(stop, looped);
    t.run();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 92 with AtomicBoolean

use of java.util.concurrent.atomic.AtomicBoolean in project tomcat by apache.

the class CoyoteAdapter method asyncDispatch.

// -------------------------------------------------------- Adapter Methods
@Override
public boolean asyncDispatch(org.apache.coyote.Request req, org.apache.coyote.Response res, SocketEvent status) throws Exception {
    Request request = (Request) req.getNote(ADAPTER_NOTES);
    Response response = (Response) res.getNote(ADAPTER_NOTES);
    if (request == null) {
        throw new IllegalStateException("Dispatch may only happen on an existing request.");
    }
    boolean success = true;
    AsyncContextImpl asyncConImpl = request.getAsyncContextInternal();
    req.getRequestProcessor().setWorkerThreadName(THREAD_NAME.get());
    try {
        if (!request.isAsync()) {
            // Error or timeout
            // Lift any suspension (e.g. if sendError() was used by an async
            // request) to allow the response to be written to the client
            response.setSuspended(false);
        }
        if (status == SocketEvent.TIMEOUT) {
            if (!asyncConImpl.timeout()) {
                asyncConImpl.setErrorState(null, false);
            }
        } else if (status == SocketEvent.ERROR) {
            // An I/O error occurred on a non-container thread which means
            // that the socket needs to be closed so set success to false to
            // trigger a close
            success = false;
            Throwable t = (Throwable) req.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
            req.getAttributes().remove(RequestDispatcher.ERROR_EXCEPTION);
            ClassLoader oldCL = null;
            try {
                oldCL = request.getContext().bind(false, null);
                if (req.getReadListener() != null) {
                    req.getReadListener().onError(t);
                }
                if (res.getWriteListener() != null) {
                    res.getWriteListener().onError(t);
                }
            } finally {
                request.getContext().unbind(false, oldCL);
            }
            if (t != null) {
                asyncConImpl.setErrorState(t, true);
            }
        }
        // Check to see if non-blocking writes or reads are being used
        if (!request.isAsyncDispatching() && request.isAsync()) {
            WriteListener writeListener = res.getWriteListener();
            ReadListener readListener = req.getReadListener();
            if (writeListener != null && status == SocketEvent.OPEN_WRITE) {
                ClassLoader oldCL = null;
                try {
                    oldCL = request.getContext().bind(false, null);
                    res.onWritePossible();
                    if (request.isFinished() && req.sendAllDataReadEvent() && readListener != null) {
                        readListener.onAllDataRead();
                    }
                } catch (Throwable t) {
                    ExceptionUtils.handleThrowable(t);
                    writeListener.onError(t);
                    success = false;
                } finally {
                    request.getContext().unbind(false, oldCL);
                }
            } else if (readListener != null && status == SocketEvent.OPEN_READ) {
                ClassLoader oldCL = null;
                try {
                    oldCL = request.getContext().bind(false, null);
                    // onDataAvailable() is not called in this case.
                    if (!request.isFinished()) {
                        readListener.onDataAvailable();
                    }
                    if (request.isFinished() && req.sendAllDataReadEvent()) {
                        readListener.onAllDataRead();
                    }
                } catch (Throwable t) {
                    ExceptionUtils.handleThrowable(t);
                    readListener.onError(t);
                    success = false;
                } finally {
                    request.getContext().unbind(false, oldCL);
                }
            }
        }
        // if the application doesn't define one)?
        if (!request.isAsyncDispatching() && request.isAsync() && response.isErrorReportRequired()) {
            connector.getService().getContainer().getPipeline().getFirst().invoke(request, response);
        }
        if (request.isAsyncDispatching()) {
            connector.getService().getContainer().getPipeline().getFirst().invoke(request, response);
            Throwable t = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
            if (t != null) {
                asyncConImpl.setErrorState(t, true);
            }
        }
        if (!request.isAsync()) {
            request.finishRequest();
            response.finishResponse();
        }
        // Check to see if the processor is in an error state. If it is,
        // bail out now.
        AtomicBoolean error = new AtomicBoolean(false);
        res.action(ActionCode.IS_ERROR, error);
        if (error.get()) {
            if (request.isAsyncCompleting()) {
                // Connection will be forcibly closed which will prevent
                // completion happening at the usual point. Need to trigger
                // call to onComplete() here.
                res.action(ActionCode.ASYNC_POST_PROCESS, null);
            }
            success = false;
        }
    } catch (IOException e) {
        success = false;
    // Ignore
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        success = false;
        log.error(sm.getString("coyoteAdapter.asyncDispatch"), t);
    } finally {
        if (!success) {
            res.setStatus(500);
        }
        // Access logging
        if (!success || !request.isAsync()) {
            long time = 0;
            if (req.getStartTime() != -1) {
                time = System.currentTimeMillis() - req.getStartTime();
            }
            if (request.getMappingData().context != null) {
                request.getMappingData().context.logAccess(request, response, time, false);
            } else {
                log(req, res, time);
            }
        }
        req.getRequestProcessor().setWorkerThreadName(null);
        // Recycle the wrapper request and response
        if (!success || !request.isAsync()) {
            request.recycle();
            response.recycle();
        }
    }
    return success;
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AsyncContextImpl(org.apache.catalina.core.AsyncContextImpl) IOException(java.io.IOException) WriteListener(javax.servlet.WriteListener) ReadListener(javax.servlet.ReadListener)

Example 93 with AtomicBoolean

use of java.util.concurrent.atomic.AtomicBoolean in project tomcat by apache.

the class CoyoteAdapter method service.

@Override
public void service(org.apache.coyote.Request req, org.apache.coyote.Response res) throws Exception {
    Request request = (Request) req.getNote(ADAPTER_NOTES);
    Response response = (Response) res.getNote(ADAPTER_NOTES);
    if (request == null) {
        // Create objects
        request = connector.createRequest();
        request.setCoyoteRequest(req);
        response = connector.createResponse();
        response.setCoyoteResponse(res);
        // Link objects
        request.setResponse(response);
        response.setRequest(request);
        // Set as notes
        req.setNote(ADAPTER_NOTES, request);
        res.setNote(ADAPTER_NOTES, response);
        // Set query string encoding
        req.getParameters().setQueryStringEncoding(connector.getURIEncoding());
    }
    if (connector.getXpoweredBy()) {
        response.addHeader("X-Powered-By", POWERED_BY);
    }
    boolean async = false;
    boolean postParseSuccess = false;
    req.getRequestProcessor().setWorkerThreadName(THREAD_NAME.get());
    try {
        // Parse and set Catalina and configuration specific
        // request parameters
        postParseSuccess = postParseRequest(req, request, res, response);
        if (postParseSuccess) {
            //check valves if we support async
            request.setAsyncSupported(connector.getService().getContainer().getPipeline().isAsyncSupported());
            // Calling the container
            connector.getService().getContainer().getPipeline().getFirst().invoke(request, response);
        }
        if (request.isAsync()) {
            async = true;
            ReadListener readListener = req.getReadListener();
            if (readListener != null && request.isFinished()) {
                // Possible the all data may have been read during service()
                // method so this needs to be checked here
                ClassLoader oldCL = null;
                try {
                    oldCL = request.getContext().bind(false, null);
                    if (req.sendAllDataReadEvent()) {
                        req.getReadListener().onAllDataRead();
                    }
                } finally {
                    request.getContext().unbind(false, oldCL);
                }
            }
            Throwable throwable = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
            // the async error process
            if (!request.isAsyncCompleting() && throwable != null) {
                request.getAsyncContextInternal().setErrorState(throwable, true);
            }
        } else {
            request.finishRequest();
            response.finishResponse();
        }
    } catch (IOException e) {
    // Ignore
    } finally {
        AtomicBoolean error = new AtomicBoolean(false);
        res.action(ActionCode.IS_ERROR, error);
        if (request.isAsyncCompleting() && error.get()) {
            // Connection will be forcibly closed which will prevent
            // completion happening at the usual point. Need to trigger
            // call to onComplete() here.
            res.action(ActionCode.ASYNC_POST_PROCESS, null);
            async = false;
        }
        // Access log
        if (!async && postParseSuccess) {
            // Log only if processing was invoked.
            // If postParseRequest() failed, it has already logged it.
            request.getMappingData().context.logAccess(request, response, System.currentTimeMillis() - req.getStartTime(), false);
        }
        req.getRequestProcessor().setWorkerThreadName(null);
        // Recycle the wrapper request and response
        if (!async) {
            request.recycle();
            response.recycle();
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) ReadListener(javax.servlet.ReadListener)

Example 94 with AtomicBoolean

use of java.util.concurrent.atomic.AtomicBoolean in project tomcat by apache.

the class Request method setReadListener.

public void setReadListener(ReadListener listener) {
    if (listener == null) {
        throw new NullPointerException(sm.getString("request.nullReadListener"));
    }
    if (getReadListener() != null) {
        throw new IllegalStateException(sm.getString("request.readListenerSet"));
    }
    // Note: This class is not used for HTTP upgrade so only need to test
    //       for async
    AtomicBoolean result = new AtomicBoolean(false);
    action(ActionCode.ASYNC_IS_ASYNC, result);
    if (!result.get()) {
        throw new IllegalStateException(sm.getString("request.notAsync"));
    }
    this.listener = listener;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 95 with AtomicBoolean

use of java.util.concurrent.atomic.AtomicBoolean in project tomcat by apache.

the class Response method setWriteListener.

public void setWriteListener(WriteListener listener) {
    if (listener == null) {
        throw new NullPointerException(sm.getString("response.nullWriteListener"));
    }
    if (getWriteListener() != null) {
        throw new IllegalStateException(sm.getString("response.writeListenerSet"));
    }
    // Note: This class is not used for HTTP upgrade so only need to test
    //       for async
    AtomicBoolean result = new AtomicBoolean(false);
    action(ActionCode.ASYNC_IS_ASYNC, result);
    if (!result.get()) {
        throw new IllegalStateException(sm.getString("response.notAsync"));
    }
    this.listener = listener;
    // written.
    if (isReady()) {
        synchronized (nonBlockingStateLock) {
            // Ensure we don't get multiple write registrations if
            // ServletOutputStream.isReady() returns false during a call to
            // onDataAvailable()
            registeredForWrite = true;
            // Need to set the fireListener flag otherwise when the
            // container tries to trigger onWritePossible, nothing will
            // happen
            fireListener = true;
        }
        action(ActionCode.DISPATCH_WRITE, null);
        if (!ContainerThreadMarker.isContainerThread()) {
            // Not on a container thread so need to execute the dispatch
            action(ActionCode.DISPATCH_EXECUTE, null);
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Aggregations

AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2412 Test (org.junit.Test)1002 CountDownLatch (java.util.concurrent.CountDownLatch)394 IOException (java.io.IOException)336 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)301 ArrayList (java.util.ArrayList)214 AtomicReference (java.util.concurrent.atomic.AtomicReference)202 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)108 Test (org.testng.annotations.Test)106 List (java.util.List)98 Ignite (org.apache.ignite.Ignite)98 AtomicLong (java.util.concurrent.atomic.AtomicLong)94 HashMap (java.util.HashMap)93 ExecutorService (java.util.concurrent.ExecutorService)90 Map (java.util.Map)88 ExecutionException (java.util.concurrent.ExecutionException)87 File (java.io.File)68 Random (java.util.Random)68 CyclicBarrier (java.util.concurrent.CyclicBarrier)68 HashSet (java.util.HashSet)63