Search in sources :

Example 1 with JmxManaged

use of org.apache.deltaspike.core.api.jmx.JmxManaged in project datawave by NationalSecurityAgency.

the class ModificationCacheBean method reloadMutableFieldCache.

/**
 * @return datawave.webservice.result.VoidResponse
 * @RequestHeader X-ProxiedEntitiesChain use when proxying request for user
 * @RequestHeader X-ProxiedIssuersChain required when using X-ProxiedEntitiesChain, specify one issuer DN per subject DN listed in X-ProxiedEntitiesChain
 * @ResponseHeader query-session-id this header and value will be in the Set-Cookie header, subsequent calls for this session will need to supply the
 *                 query-session-id header in the request in a Cookie header or as a query parameter
 * @ResponseHeader X-OperationTimeInMS time spent on the server performing the operation, does not account for network or result serialization
 * @HTTP 200 success
 * @HTTP 202 if asynch is true - see Location response header for the job URI location
 * @HTTP 400 invalid or missing parameter
 * @HTTP 500 internal server error
 */
@GET
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff" })
@Path("/reloadCache")
@GZIP
@JmxManaged
public VoidResponse reloadMutableFieldCache() {
    this.clearCache();
    log.trace("cleared cache");
    final VoidResponse resp = new VoidResponse();
    Connector con = null;
    BatchScanner s = null;
    try {
        Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
        log.trace("getting mutable list from table " + this.modificationConfiguration.getTableName());
        log.trace("modificationConfiguration.getPoolName() = " + modificationConfiguration.getPoolName());
        con = connectionFactory.getConnection(modificationConfiguration.getPoolName(), Priority.ADMIN, trackingMap);
        log.trace("got connection");
        s = ScannerHelper.createBatchScanner(con, this.modificationConfiguration.getTableName(), Collections.singleton(con.securityOperations().getUserAuthorizations(con.whoami())), 8);
        s.setRanges(Collections.singleton(new Range()));
        s.fetchColumnFamily(MODIFICATION_COLUMN);
        for (Entry<Key, Value> e : s) {
            // Field name is in the row and datatype is in the colq.
            String datatype = e.getKey().getColumnQualifier().toString();
            log.trace("datatype = " + datatype);
            String fieldName = e.getKey().getRow().toString();
            log.trace("fieldname = " + fieldName);
            if (null == cache.get(datatype))
                cache.put(datatype, new HashSet<>());
            cache.get(datatype).add(fieldName);
        }
        log.trace("cache size = " + cache.size());
        for (Entry<String, Set<String>> e : cache.entrySet()) {
            log.trace("datatype = " + e.getKey() + ", fieldcount = " + e.getValue().size());
        }
    } catch (Exception e) {
        log.error("Error during initialization of ModificationCacheBean", e);
        throw new EJBException("Error during initialization of ModificationCacheBean", e);
    } finally {
        if (null != s)
            s.close();
        try {
            connectionFactory.returnConnection(con);
        } catch (Exception e) {
            log.error("Error returning connection to pool", e);
        }
    }
    return resp;
}
Also used : Connector(org.apache.accumulo.core.client.Connector) HashSet(java.util.HashSet) Set(java.util.Set) BatchScanner(org.apache.accumulo.core.client.BatchScanner) Range(org.apache.accumulo.core.data.Range) EJBException(javax.ejb.EJBException) VoidResponse(datawave.webservice.result.VoidResponse) Value(org.apache.accumulo.core.data.Value) EJBException(javax.ejb.EJBException) Key(org.apache.accumulo.core.data.Key) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) JmxManaged(org.apache.deltaspike.core.api.jmx.JmxManaged) GET(javax.ws.rs.GET) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 2 with JmxManaged

use of org.apache.deltaspike.core.api.jmx.JmxManaged in project datawave by NationalSecurityAgency.

the class HealthBean method waitForQueryCompletion.

/**
 * This method initiates a shutdown. This will set internal state such that the {@link #health()} method will return a {@link Status#SERVICE_UNAVAILABLE}
 * error to prevent new queries from coming in. After that, it will wait up to {@code timeoutMinutes} for queries to complete before initiating a shutdown
 * of the web server.
 *
 * <strong>NOTE:</strong> This method is restricted to users calling from the local host, or via the JMX interface.
 *
 * TODO: Is there a way to "cancel" any requests that are waiting for an Accumulo connection? It would be nice if such requests could be rejected somehow
 * and the load balancer could automatically redirect to a different web server.
 *
 * @param timeoutMinutes
 *            the number of minutes to wait for queries to complete before continuing with the shutdown operation anyway
 * @return a message indicating whether or not shutdown was attempted
 */
@GET
@Path("/shutdown")
@JmxManaged
public Response waitForQueryCompletion(@QueryParam("timeoutMinutes") @DefaultValue("75") int timeoutMinutes, @Context HttpServletRequest request) {
    GenericResponse<String> response = new GenericResponse<>();
    // We're only allowed to call shutdown from the loopback interface.
    if (!"127.0.0.1".equals(request.getRemoteAddr())) {
        LOG.error("Shutdown requested from {}. Denying access since the request was not from localhost.", request.getRemoteAddr());
        response.setResult("Shutdown calls must be made on the local host.");
        return Response.status(Status.FORBIDDEN).entity(response).build();
    }
    LOG.warn("Shutdown requested from {}. Waiting up to {} minutes for queries to complete.", request.getRemoteAddr(), timeoutMinutes);
    // Wait for queries to complete
    long timeoutMillis = TimeUnit.MINUTES.toMillis(timeoutMinutes);
    shutdownInProgress = true;
    status = "drain";
    long startTime = System.currentTimeMillis();
    int connectionUsage;
    while ((connectionUsage = accumuloConnectionFactoryBean.getConnectionUsagePercent()) > 0) {
        long delta = System.currentTimeMillis() - startTime;
        if (delta > timeoutMillis) {
            LOG.warn("Timeout of {} minutes exceeded while waiting for queries to complete. Shutting down anyway.", timeoutMinutes);
            break;
        }
        LOG.info("Connection usage is {}%. Waiting for queries to complete.", connectionUsage);
        try {
            Thread.sleep(queryCompletionWaitIntervalMillis);
        } catch (InterruptedException e) {
            LOG.warn("Interrupted while waiting for queries to complete.");
        }
    }
    if (connectionUsage <= 0) {
        response.setResult("All queries completed. Shutting down.");
    } else {
        response.setResult("Gave up waiting for queries to complete. Shutting down with pool usage percentage of " + connectionUsage + ".");
    }
    // Initiate a server shutdown using the management JMX bean
    try {
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        ObjectName objectName = new ObjectName("jboss.as:management-root=server");
        mBeanServer.invoke(objectName, "shutdown", new Object[] { false, 0, 0 }, new String[] { boolean.class.getName(), int.class.getName(), int.class.getName() });
    } catch (MalformedObjectNameException | ReflectionException | InstanceNotFoundException | MBeanException e) {
        LOG.warn("Error shutting down: {}", e);
    }
    return Response.ok().entity(response).build();
}
Also used : ReflectionException(javax.management.ReflectionException) MalformedObjectNameException(javax.management.MalformedObjectNameException) GenericResponse(datawave.webservice.result.GenericResponse) InstanceNotFoundException(javax.management.InstanceNotFoundException) ObjectName(javax.management.ObjectName) MBeanException(javax.management.MBeanException) MBeanServer(javax.management.MBeanServer) Path(javax.ws.rs.Path) JmxManaged(org.apache.deltaspike.core.api.jmx.JmxManaged) GET(javax.ws.rs.GET)

Example 3 with JmxManaged

use of org.apache.deltaspike.core.api.jmx.JmxManaged in project datawave by NationalSecurityAgency.

the class AccumuloConnectionFactoryBean method getConnectionUsagePercent.

@PermitAll
@JmxManaged
public int getConnectionUsagePercent() {
    double maxPercentage = 0.0;
    for (Entry<String, Map<Priority, AccumuloConnectionPool>> entry : pools.entrySet()) {
        for (Entry<Priority, AccumuloConnectionPool> poolEntry : entry.getValue().entrySet()) {
            // Don't include ADMIN priority connections when computing a usage percentage
            if (Priority.ADMIN.equals(poolEntry.getKey()))
                continue;
            MutableInt maxActive = new MutableInt();
            MutableInt numActive = new MutableInt();
            MutableInt numWaiting = new MutableInt();
            MutableInt unused = new MutableInt();
            poolEntry.getValue().getConnectionPoolStats(maxActive, numActive, unused, unused, numWaiting);
            double percentage = (numActive.doubleValue() + numWaiting.doubleValue()) / maxActive.doubleValue();
            if (percentage > maxPercentage) {
                maxPercentage = percentage;
            }
        }
    }
    return (int) (maxPercentage * 100);
}
Also used : MutableInt(org.apache.commons.lang.mutable.MutableInt) Map(java.util.Map) HashMap(java.util.HashMap) JmxManaged(org.apache.deltaspike.core.api.jmx.JmxManaged) PermitAll(javax.annotation.security.PermitAll)

Aggregations

JmxManaged (org.apache.deltaspike.core.api.jmx.JmxManaged)3 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 GenericResponse (datawave.webservice.result.GenericResponse)1 VoidResponse (datawave.webservice.result.VoidResponse)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 PermitAll (javax.annotation.security.PermitAll)1 EJBException (javax.ejb.EJBException)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 MBeanException (javax.management.MBeanException)1 MBeanServer (javax.management.MBeanServer)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 ObjectName (javax.management.ObjectName)1 ReflectionException (javax.management.ReflectionException)1 Produces (javax.ws.rs.Produces)1 BatchScanner (org.apache.accumulo.core.client.BatchScanner)1 Connector (org.apache.accumulo.core.client.Connector)1