Search in sources :

Example 6 with Event

use of io.fabric8.kubernetes.api.model.Event in project fabric8 by jboss-fuse.

the class Log4jLogQuery method filterLogResults.

protected LogResults filterLogResults(Predicate<LogEvent> predicate, int maxCount) {
    int matched = 0;
    long from = Long.MAX_VALUE;
    long to = Long.MIN_VALUE;
    List<LogEvent> list = new ArrayList<LogEvent>();
    Iterable<LoggingEvent> elements = getEvents().getElements();
    for (LoggingEvent element : elements) {
        LogEvent logEvent = toLogEvent(element);
        long timestamp = element.getTimeStamp();
        if (timestamp > to) {
            to = timestamp;
        }
        if (timestamp < from) {
            from = timestamp;
        }
        if (logEvent != null) {
            if (predicate == null || predicate.matches(logEvent)) {
                list.add(logEvent);
                matched += 1;
                if (maxCount > 0 && matched >= maxCount) {
                    break;
                }
            }
        }
    }
    LogResults results = new LogResults();
    results.setEvents(list);
    if (from < Long.MAX_VALUE) {
        results.setFromTimestamp(from);
    }
    if (to > Long.MIN_VALUE) {
        results.setToTimestamp(to);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Requested " + maxCount + " logging items. returning " + results.getEvents().size() + " event(s) from a possible " + getEvents().size());
    }
    return results;
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) LogResults(io.fabric8.insight.log.LogResults) LogEvent(io.fabric8.insight.log.LogEvent) ArrayList(java.util.ArrayList)

Example 7 with Event

use of io.fabric8.kubernetes.api.model.Event in project fabric8 by jboss-fuse.

the class Log4jLogQuery method createPredicate.

private Predicate<LogEvent> createPredicate(LogFilter filter) {
    if (filter == null) {
        return null;
    }
    final List<Predicate<LogEvent>> predicates = new ArrayList<Predicate<LogEvent>>();
    final Set<String> levels = filter.getLevelsSet();
    if (levels.size() > 0) {
        predicates.add(new Predicate<LogEvent>() {

            @Override
            public boolean matches(LogEvent event) {
                String level = event.getLevel();
                return level != null && levels.contains(level.toString());
            }
        });
    }
    final Long before = filter.getBeforeTimestamp();
    if (before != null) {
        final Date date = new Date(before);
        predicates.add(new Predicate<LogEvent>() {

            @Override
            public boolean matches(LogEvent event) {
                Date time = event.getTimestamp();
                return time != null && time.before(date);
            }
        });
    }
    final Long after = filter.getAfterTimestamp();
    if (after != null) {
        final Date date = new Date(after);
        predicates.add(new Predicate<LogEvent>() {

            @Override
            public boolean matches(LogEvent event) {
                Date time = event.getTimestamp();
                return time != null && time.after(date);
            }
        });
    }
    final String matchesText = filter.getMatchesText();
    if (matchesText != null && matchesText.length() > 0) {
        predicates.add(new Predicate<LogEvent>() {

            @Override
            public boolean matches(LogEvent event) {
                if (contains(matchesText, event.getClassName(), event.getMessage(), event.getLogger(), event.getThread())) {
                    return true;
                }
                String[] throwableStrRep = event.getException();
                if (throwableStrRep != null && contains(matchesText, throwableStrRep)) {
                    return true;
                }
                Map properties = event.getProperties();
                if (properties != null && contains(matchesText, properties.toString())) {
                    return true;
                }
                return false;
            }
        });
    }
    if (predicates.size() == 0) {
        return null;
    } else if (predicates.size() == 1) {
        return predicates.get(0);
    } else {
        return new Predicate<LogEvent>() {

            @Override
            public String toString() {
                return "AndPredicate" + predicates;
            }

            @Override
            public boolean matches(LogEvent event) {
                for (Predicate<LogEvent> predicate : predicates) {
                    if (!predicate.matches(event)) {
                        return false;
                    }
                }
                return true;
            }
        };
    }
}
Also used : LogEvent(io.fabric8.insight.log.LogEvent) ArrayList(java.util.ArrayList) Map(java.util.Map) Date(java.util.Date) Predicate(io.fabric8.insight.log.support.Predicate)

Example 8 with Event

use of io.fabric8.kubernetes.api.model.Event in project fabric8 by jboss-fuse.

the class GitPatchManagementServiceImpl method checkPendingPatches.

@Override
public void checkPendingPatches() {
    File[] pendingPatches = patchesDir.listFiles(new FileFilter() {

        @Override
        public boolean accept(File pathname) {
            return pathname.exists() && pathname.getName().endsWith(".pending");
        }
    });
    if (pendingPatches == null || pendingPatches.length == 0) {
        return;
    }
    final String dataCache = systemContext.getProperty("org.osgi.framework.storage");
    for (File pending : pendingPatches) {
        try {
            Pending what = Pending.valueOf(FileUtils.readFileToString(pending));
            final String prefix = what == Pending.ROLLUP_INSTALLATION ? "install" : "rollback";
            String name = pending.getName().replaceFirst("\\.pending$", "");
            if (isStandaloneChild()) {
                if (name.endsWith("." + System.getProperty("karaf.name") + ".patch")) {
                    name = name.replaceFirst("\\." + System.getProperty("karaf.name"), "");
                } else {
                    continue;
                }
            }
            File patchFile = new File(pending.getParentFile(), name);
            if (!patchFile.isFile()) {
                Activator.log(LogService.LOG_INFO, "Ignoring patch result file: " + patchFile.getName());
                continue;
            }
            PatchData patchData = PatchData.load(new FileInputStream(patchFile));
            Patch patch = loadPatch(new PatchDetailsRequest(patchData.getId()));
            String dataFilesName = patchData.getId() + ".datafiles";
            if (isStandaloneChild()) {
                dataFilesName = patchData.getId() + "." + System.getProperty("karaf.name") + ".datafiles";
            }
            final File dataFilesBackupDir = new File(pending.getParentFile(), dataFilesName);
            final Properties backupProperties = new Properties();
            FileInputStream inStream = new FileInputStream(new File(dataFilesBackupDir, "backup-" + prefix + ".properties"));
            backupProperties.load(inStream);
            IOUtils.closeQuietly(inStream);
            // maybe one of those bundles has data directory to restore?
            for (Bundle b : systemContext.getBundles()) {
                if (b.getSymbolicName() != null) {
                    String key = String.format("%s$$%s", stripSymbolicName(b.getSymbolicName()), b.getVersion().toString());
                    if (backupProperties.containsKey(key)) {
                        String backupDirName = backupProperties.getProperty(key);
                        File backupDir = new File(dataFilesBackupDir, prefix + "/" + backupDirName + "/data");
                        restoreDataDirectory(dataCache, b, backupDir);
                        // we no longer want to restore this dir
                        backupProperties.remove(key);
                    }
                }
            }
            // 2. We can however have more bundle data backups - we'll restore them after each bundle
            // is INSTALLED and we'll use listener for this
            BundleListener bundleListener = new SynchronousBundleListener() {

                @Override
                public void bundleChanged(BundleEvent event) {
                    Bundle b = event.getBundle();
                    if (event.getType() == BundleEvent.INSTALLED && b.getSymbolicName() != null) {
                        String key = String.format("%s$$%s", stripSymbolicName(b.getSymbolicName()), b.getVersion().toString());
                        if (backupProperties.containsKey(key)) {
                            String backupDirName = backupProperties.getProperty(key);
                            File backupDir = new File(dataFilesBackupDir, prefix + "/" + backupDirName + "/data");
                            restoreDataDirectory(dataCache, b, backupDir);
                        }
                    }
                }
            };
            systemContext.addBundleListener(bundleListener);
            pendingPatchesListeners.put(patchData.getId(), bundleListener);
        } catch (Exception e) {
            Activator.log(LogService.LOG_ERROR, null, e.getMessage(), e, true);
        }
    }
}
Also used : PatchData(io.fabric8.patch.management.PatchData) Bundle(org.osgi.framework.Bundle) Properties(java.util.Properties) PatchDetailsRequest(io.fabric8.patch.management.PatchDetailsRequest) FileInputStream(java.io.FileInputStream) PatchException(io.fabric8.patch.management.PatchException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) FileFilter(java.io.FileFilter) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) ManagedPatch(io.fabric8.patch.management.ManagedPatch) Patch(io.fabric8.patch.management.Patch) Pending(io.fabric8.patch.management.Pending) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener)

Example 9 with Event

use of io.fabric8.kubernetes.api.model.Event in project fabric8 by jboss-fuse.

the class HttpGatewayConnectionTimeoutTest method testConnectionTimeout.

@Test
public void testConnectionTimeout() throws Exception {
    startRestEndpoint();
    startHttpGateway();
    LOG.info("Requesting...");
    final FutureHandler<HttpClientResponse> future = new FutureHandler<>();
    vertx.createHttpClient().setHost("localhost").setPort(8080).get("/hello/world?wsdl", new Handler<HttpClientResponse>() {

        @Override
        public void handle(HttpClientResponse event) {
            future.handle(event);
        }
    }).end();
    HttpClientResponse response = future.await();
    assertEquals(response.statusCode(), 504);
    stopHttpGateway();
    stopVertx();
}
Also used : HttpClientResponse(org.vertx.java.core.http.HttpClientResponse) HttpGatewayHandler(io.fabric8.gateway.handlers.http.HttpGatewayHandler) FutureHandler(io.fabric8.gateway.handlers.detecting.FutureHandler) Handler(org.vertx.java.core.Handler) FutureHandler(io.fabric8.gateway.handlers.detecting.FutureHandler) Test(org.junit.Test)

Example 10 with Event

use of io.fabric8.kubernetes.api.model.Event in project fabric8 by jboss-fuse.

the class HttpGatewayRequestTimeoutTest method testRequestTimeout.

@Test
public void testRequestTimeout() throws Exception {
    startRestEndpoint();
    startHttpGateway();
    LOG.info("Requesting...");
    final FutureHandler<HttpClientResponse> future = new FutureHandler<>();
    vertx.createHttpClient().setHost("localhost").setPort(8080).get("/hello/world?wsdl", new Handler<HttpClientResponse>() {

        @Override
        public void handle(HttpClientResponse event) {
            future.handle(event);
        }
    }).end();
    HttpClientResponse response = future.await();
    assertEquals(response.statusCode(), 504);
    stopHttpGateway();
    stopVertx();
}
Also used : HttpClientResponse(org.vertx.java.core.http.HttpClientResponse) HttpGatewayHandler(io.fabric8.gateway.handlers.http.HttpGatewayHandler) Handler(org.vertx.java.core.Handler) FutureHandler(io.fabric8.gateway.handlers.detecting.FutureHandler) FutureHandler(io.fabric8.gateway.handlers.detecting.FutureHandler) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)14 IOException (java.io.IOException)11 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)8 ArrayList (java.util.ArrayList)6 ConnectionParameters (io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters)5 File (java.io.File)5 Map (java.util.Map)5 CuratorFramework (org.apache.curator.framework.CuratorFramework)5 Logger (org.slf4j.Logger)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 LoggerFactory (org.slf4j.LoggerFactory)4 ServiceDTO (io.fabric8.gateway.ServiceDTO)3 FutureHandler (io.fabric8.gateway.handlers.detecting.FutureHandler)3 HttpGatewayHandler (io.fabric8.gateway.handlers.http.HttpGatewayHandler)3 LogEvent (io.fabric8.insight.log.LogEvent)3 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)3 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 HashMap (java.util.HashMap)3 ChildData (org.apache.curator.framework.recipes.cache.ChildData)3