Search in sources :

Example 51 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project fabric8 by jboss-fuse.

the class Subsystem method downloadBundles.

@SuppressWarnings("InfiniteLoopStatement")
public void downloadBundles(DownloadManager manager, MetadataBuilder builder, Set<String> overrides, String featureResolutionRange) throws Exception {
    for (Subsystem child : children) {
        child.downloadBundles(manager, builder, overrides, featureResolutionRange);
    }
    final Map<String, ResourceImpl> bundles = new ConcurrentHashMap<>();
    final Downloader downloader = manager.createDownloader();
    final Map<BundleInfo, Conditional> infos = new HashMap<>();
    if (feature != null) {
        for (Conditional cond : feature.getConditional()) {
            for (final BundleInfo bi : cond.getBundles()) {
                infos.put(bi, cond);
            }
        }
        for (BundleInfo bi : feature.getBundles()) {
            infos.put(bi, null);
        }
    }
    ResourceBuilderCallback callback = new ResourceBuilderCallback(bundles, builder);
    for (Map.Entry<BundleInfo, Conditional> entry : infos.entrySet()) {
        final BundleInfo bi = entry.getKey();
        final String loc = bi.getLocation();
        downloader.download(loc, callback);
    }
    for (Clause bundle : Parser.parseClauses(this.bundles.toArray(new String[this.bundles.size()]))) {
        final String loc = bundle.getName();
        downloader.download(loc, callback);
    }
    for (final FabricBundle fabricBundle : fabricBundles) {
        final String loc = fabricBundle.getLocation();
        downloader.download(loc, callback);
    }
    for (String override : overrides) {
        final String loc = Overrides.extractUrl(override);
        downloader.download(loc, callback);
    }
    downloader.await();
    Overrides.override(bundles, overrides);
    if (feature != null) {
        // Add conditionals
        Map<Conditional, Resource> resConds = new HashMap<>();
        for (Conditional cond : feature.getConditional()) {
            FeatureResource resCond = FeatureResource.build(feature, cond, featureResolutionRange, bundles);
            addIdentityRequirement(this, resCond, false);
            addIdentityRequirement(resCond, this, true);
            installable.add(resCond);
            resConds.put(cond, resCond);
        }
        // Add features
        FeatureResource resFeature = FeatureResource.build(feature, featureResolutionRange, bundles);
        addIdentityRequirement(resFeature, this);
        installable.add(resFeature);
        // Add dependencies
        for (Map.Entry<BundleInfo, Conditional> entry : infos.entrySet()) {
            final BundleInfo bi = entry.getKey();
            final String loc = bi.getLocation();
            final Conditional cond = entry.getValue();
            ResourceImpl res = bundles.get(loc);
            int sl = bi.getStartLevel() <= 0 ? feature.getStartLevel() : bi.getStartLevel();
            if (bi.isDependency()) {
                addDependency(res, false, bi.isStart(), sl);
            } else {
                doAddDependency(res, cond == null, bi.isStart(), sl);
            }
            if (cond != null) {
                addIdentityRequirement(res, resConds.get(cond), true);
            }
        }
    }
    for (Clause bundle : Parser.parseClauses(this.bundles.toArray(new String[this.bundles.size()]))) {
        final String loc = bundle.getName();
        boolean dependency = Boolean.parseBoolean(bundle.getAttribute("dependency"));
        boolean start = bundle.getAttribute("start") == null || Boolean.parseBoolean(bundle.getAttribute("start"));
        int startLevel = 0;
        try {
            startLevel = Integer.parseInt(bundle.getAttribute("start-level"));
        } catch (NumberFormatException e) {
        // Ignore
        }
        if (dependency) {
            addDependency(bundles.get(loc), false, start, startLevel);
        } else {
            doAddDependency(bundles.get(loc), true, start, startLevel);
            addIdentityRequirement(this, bundles.get(loc));
        }
    }
    for (final FabricBundle fabricBundle : fabricBundles) {
        final String loc = fabricBundle.getLocation();
        boolean dependency = Boolean.parseBoolean(fabricBundle.getProperty("dependency"));
        boolean start = fabricBundle.getProperty("start") == null || Boolean.parseBoolean(fabricBundle.getProperty("start"));
        int startLevel = 0;
        try {
            startLevel = Integer.parseInt(fabricBundle.getProperty("start-level"));
        } catch (NumberFormatException e) {
        // Ignore
        }
        if (dependency) {
            addDependency(bundles.get(loc), false, start, startLevel);
        } else {
            doAddDependency(bundles.get(loc), true, start, startLevel);
            addIdentityRequirement(this, bundles.get(loc));
        }
    }
    // Compute dependencies
    for (DependencyInfo info : dependencies.values()) {
        installable.add(info.resource);
        addIdentityRequirement(info.resource, this, info.mandatory);
    }
}
Also used : FeatureResource(io.fabric8.agent.resolver.FeatureResource) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Resource(org.osgi.resource.Resource) FeatureResource(io.fabric8.agent.resolver.FeatureResource) Downloader(io.fabric8.agent.download.Downloader) Conditional(io.fabric8.agent.model.Conditional) ResourceImpl(io.fabric8.agent.resolver.ResourceImpl) BundleInfo(io.fabric8.agent.model.BundleInfo) Clause(org.apache.felix.utils.manifest.Clause) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 52 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project fabric8 by jboss-fuse.

the class DummyBatchingProgressMonitor method activateInternal.

private void activateInternal() throws Exception {
    LOGGER.info("Starting up GitDataStore " + this);
    // Call the bootstrap {@link DataStoreTemplate}
    DataStoreTemplate template = runtimeProperties.get().removeRuntimeAttribute(DataStoreTemplate.class);
    if (template != null) {
        // Do the initial commit and set the root tag
        Ref rootTag = getGit().getRepository().getRef(GitHelpers.ROOT_TAG);
        if (rootTag == null) {
            getGit().commit().setMessage("First Commit").setCommitter("fabric", "user@fabric").call();
            getGit().tag().setName(GitHelpers.ROOT_TAG).setMessage("Tag the root commit").call();
        }
        LOGGER.debug("Running datastore bootstrap template: " + template);
        template.doWith(this, dataStore.get());
    }
    // Setup proxy service
    GitProxyService proxyService = gitProxyService.get();
    defaultProxySelector = ProxySelector.getDefault();
    // authenticator disabled, until properly tested it does not affect others, as Authenticator is static in the JVM
    // Authenticator.setDefault(new FabricGitLocalHostAuthenticator(proxyService));
    ProxySelector fabricProxySelector = new FabricGitLocalHostProxySelector(defaultProxySelector, proxyService);
    ProxySelector.setDefault(fabricProxySelector);
    LOGGER.debug("Setting up FabricProxySelector: {}", fabricProxySelector);
    if (gitRemoteUrl != null) {
        gitListener.runRemoteUrlChanged(gitRemoteUrl);
        remoteUrl = gitRemoteUrl;
    } else {
        gitService.get().addGitListener(gitListener);
        remoteUrl = gitService.get().getRemoteUrl();
        if (remoteUrl != null) {
            gitListener.runRemoteUrlChanged(remoteUrl);
        }
    }
    // Get initial versions
    getInitialVersions();
    // poll logic in case of remote git repo
    if (gitRemoteUrl != null) {
        // i need this old logic in case of remote repos
        LOGGER.info("Starting to pull from remote git repository every {} millis", gitRemotePollInterval);
        threadPool.scheduleWithFixedDelay(new Runnable() {

            @Override
            public void run() {
                LockHandle writeLock = aquireWriteLock();
                try {
                    LOGGER.trace("Performing timed pull");
                    doPullInternal();
                    LOGGER.debug("Performed timed pull from external git repo");
                } catch (Throwable e) {
                    LOGGER.debug("Error during performed timed pull/push due " + e.getMessage(), e);
                    LOGGER.warn("Error during performed timed pull/push due " + e.getMessage() + ". This exception is ignored.");
                } finally {
                    writeLock.unlock();
                }
            }

            @Override
            public String toString() {
                return "TimedPushTask";
            }
        }, 1000, gitRemotePollInterval, TimeUnit.MILLISECONDS);
    }
    LOGGER.info("Using ZooKeeper SharedCount to react when master git repo is changed, so we can do a git pull to the local git repo.");
    counter = new SharedCount(curator.get(), ZkPath.GIT_TRIGGER.getPath(), 0);
    counter.addListener(new SharedCountListener() {

        @Override
        public void countHasChanged(final SharedCountReader sharedCountReader, final int value) throws Exception {
            Runnable task = new Runnable() {

                @Override
                public void run() {
                    doPullInternal();
                }
            };
            if (gitRandomFetchDelay == 0) {
                LOGGER.debug("Watch counter updated to " + value + ", scheduling immediate pull");
                threadPool.submit(task);
            } else {
                int delay = RND.nextInt(gitRandomFetchDelay) + 1;
                LOGGER.debug("Watch counter updated to " + value + ", scheduling pull with random delay=" + delay + "s");
                threadPool.schedule(task, delay, TimeUnit.SECONDS);
            }
        }

        @Override
        public void stateChanged(CuratorFramework curatorFramework, ConnectionState connectionState) {
            switch(connectionState) {
                case SUSPENDED:
                case READ_ONLY:
                case LOST:
                    // do nothing
                    break;
                case CONNECTED:
                case RECONNECTED:
                    LOGGER.info("Shared Counter (Re)connected, doing a pull");
                    doPullInternal();
                    break;
            }
        }
    });
    try {
        counter.start();
    } catch (KeeperException.NotReadOnlyException ex) {
    // In read only mode the counter is not going to start.
    // If the connection is reestablished the component will reactivate.
    // We need to catch this error so that the component gets activated.
    }
    if (gitGcOnLoad) {
        LockHandle writeLock = aquireWriteLock();
        try {
            GitOperation<Void> gitop = new GitOperation<Void>() {

                public Void call(Git git, GitContext context) throws Exception {
                    long before = System.currentTimeMillis();
                    try {
                        git.gc().call();
                        LOGGER.debug("git gc took " + ((System.currentTimeMillis() - before)) + " ms.");
                    } catch (GitAPIException e) {
                        LOGGER.debug("git gc threw an exception!", e);
                    }
                    return null;
                }
            };
            executeInternal(new GitContext(), null, gitop);
        } finally {
            writeLock.unlock();
        }
    }
    // failing to activate the component
    if (readWriteLock.isWriteLockedByCurrentThread() || readWriteLock.getWriteHoldCount() == 0) {
        try {
            // let's delegate to other thread in order to free MCF thread
            // ENTESB-7843: there's a problem when jetty is configured with TLS and keystore location using
            // profile: URI. while Jetty continues to be configured if profile:jetty.xml isn't available
            // (and it isn't initially), it fails trying to access keystore via profile: URI.
            // we should optimistically assume we can pull, but there's nothing wrong if we can't
            // - we'll be able to pull later
            threadPoolInitial.execute(new Runnable() {

                @Override
                public void run() {
                    doPullInternal(5);
                }
            });
        } catch (IllegalStateException e) {
            LOGGER.info("Another thread acquired write lock and GitDataStore can't pull from remote repository.");
        }
    } else {
        LOGGER.info("Another thread is keeping git write lock. GitDataStore will continue activation.");
    }
}
Also used : SharedCount(org.apache.curator.framework.recipes.shared.SharedCount) ProxySelector(java.net.ProxySelector) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) CuratorFramework(org.apache.curator.framework.CuratorFramework) DataStoreTemplate(io.fabric8.api.DataStoreTemplate) SharedCountListener(org.apache.curator.framework.recipes.shared.SharedCountListener) GitProxyService(io.fabric8.git.GitProxyService) LockHandle(io.fabric8.api.LockHandle) SharedCountReader(org.apache.curator.framework.recipes.shared.SharedCountReader) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) FabricException(io.fabric8.api.FabricException) KeeperException(org.apache.zookeeper.KeeperException) MalformedURLException(java.net.MalformedURLException) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) GitContext(io.fabric8.api.GitContext) ConnectionState(org.apache.curator.framework.state.ConnectionState) KeeperException(org.apache.zookeeper.KeeperException)

Example 53 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project fabric8 by jboss-fuse.

the class InvocationTest method testUnderLoadSyncObject.

@Test(timeout = 30 * 1000)
public void testUnderLoadSyncObject() throws Exception {
    HashMap<String, SerializationStrategy> map = new HashMap<String, SerializationStrategy>();
    DispatchQueue queue = Dispatch.createQueue();
    ServerInvokerImpl server = new ServerInvokerImpl("tcp://localhost:0", queue, map);
    server.start();
    ClientInvokerImpl client = new ClientInvokerImpl(queue, map);
    client.start();
    try {
        final HelloImpl helloImpl = new HelloImpl();
        server.registerService("service-id", new ServerInvoker.ServiceFactory() {

            public Object get() {
                return helloImpl;
            }

            public void unget() {
            }
        }, HelloImpl.class.getClassLoader());
        InvocationHandler handler = client.getProxy(server.getConnectAddress(), "service-id", HelloImpl.class.getClassLoader());
        final Hello hello = (Hello) Proxy.newProxyInstance(HelloImpl.class.getClassLoader(), new Class[] { Hello.class }, handler);
        assertEquals("Hello World!", hello.helloworld());
        final AtomicInteger requests = new AtomicInteger(0);
        final AtomicInteger failures = new AtomicInteger(0);
        final long[] latencies = new long[BENCHMARK_CLIENTS * BENCHMARK_INVOCATIONS_PER_CLIENT];
        final long start = System.nanoTime();
        Thread[] threads = new Thread[BENCHMARK_CLIENTS];
        for (int t = 0; t < BENCHMARK_CLIENTS; t++) {
            final int thread_idx = t;
            threads[t] = new Thread() {

                public void run() {
                    for (int i = 0; i < BENCHMARK_INVOCATIONS_PER_CLIENT; i++) {
                        try {
                            requests.incrementAndGet();
                            String response;
                            final long start = System.nanoTime();
                            response = hello.hello("Fabric");
                            final long end = System.nanoTime();
                            latencies[(thread_idx * BENCHMARK_INVOCATIONS_PER_CLIENT) + i] = end - start;
                            assertEquals("Hello Fabric!", response);
                        } catch (Throwable t) {
                            latencies[(thread_idx * BENCHMARK_INVOCATIONS_PER_CLIENT) + i] = -1;
                            failures.incrementAndGet();
                            if (t instanceof UndeclaredThrowableException) {
                                t = ((UndeclaredThrowableException) t).getUndeclaredThrowable();
                            }
                            System.err.println("Error: " + t.getClass().getName() + (t.getMessage() != null ? " (" + t.getMessage() + ")" : ""));
                        }
                    }
                }
            };
            threads[t].start();
        }
        for (int t = 0; t < BENCHMARK_CLIENTS; t++) {
            threads[t].join();
        }
        final long end = System.nanoTime();
        long latency_sum = 0;
        for (int t = 0; t < latencies.length; t++) {
            if (latencies[t] != -1) {
                latency_sum += latencies[t];
            }
        }
        double latency_avg = ((latency_sum * 1.0d) / requests.get()) / MILLIS_IN_A_NANO;
        double request_rate = ((requests.get() * 1.0d) / (end - start)) * SECONDS_IN_A_NANO;
        System.err.println(String.format("Requests/Second: %,.2f", request_rate));
        System.err.println(String.format("Average request latency: %,.2f ms", latency_avg));
        System.err.println("Error Ratio: " + failures.get() + " / " + requests.get());
    } finally {
        server.stop();
        client.stop();
    }
}
Also used : ServerInvoker(io.fabric8.dosgi.io.ServerInvoker) HashMap(java.util.HashMap) InvocationHandler(java.lang.reflect.InvocationHandler) ClientInvokerImpl(io.fabric8.dosgi.tcp.ClientInvokerImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ServerInvokerImpl(io.fabric8.dosgi.tcp.ServerInvokerImpl) DispatchQueue(org.fusesource.hawtdispatch.DispatchQueue) Test(org.junit.Test)

Example 54 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project fabric8 by jboss-fuse.

the class GroupTest method testGroupClose.

// Tests that if close() is executed right after start(), there are no left over entries.
// (see  https://github.com/jboss-fuse/fuse/issues/133)
@Test
public void testGroupClose() throws Exception {
    int port = findFreePort();
    NIOServerCnxnFactory cnxnFactory = startZooKeeper(port);
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString("localhost:" + port).connectionTimeoutMs(6000).sessionTimeoutMs(6000).retryPolicy(new RetryNTimes(10, 100));
    CuratorFramework curator = builder.build();
    curator.start();
    curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    String groupNode = "/singletons/test" + System.currentTimeMillis();
    curator.create().creatingParentsIfNeeded().forPath(groupNode);
    for (int i = 0; i < 100; i++) {
        ZooKeeperGroup<NodeState> group = new ZooKeeperGroup<NodeState>(curator, groupNode, NodeState.class);
        group.add(listener);
        group.update(new NodeState("foo"));
        group.start();
        group.close();
        List<String> entries = curator.getChildren().forPath(groupNode);
        assertTrue(entries.isEmpty() || group.isUnstable());
        if (group.isUnstable()) {
            // let's wait for session timeout
            curator.close();
            curator = builder.build();
            curator.start();
            curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
        }
    }
    curator.close();
    cnxnFactory.shutdown();
    cnxnFactory.join();
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) ZooKeeperGroup(io.fabric8.groups.internal.ZooKeeperGroup) Test(org.junit.Test)

Example 55 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project fabric8 by jboss-fuse.

the class GroupTest method testOrder.

@Test
public void testOrder() throws Exception {
    int port = findFreePort();
    CuratorFramework curator = CuratorFrameworkFactory.builder().connectString("localhost:" + port).retryPolicy(new RetryNTimes(10, 100)).build();
    curator.start();
    final String path = "/singletons/test/Order" + System.currentTimeMillis();
    ArrayList<ZooKeeperGroup> members = new ArrayList<ZooKeeperGroup>();
    for (int i = 0; i < 4; i++) {
        ZooKeeperGroup<NodeState> group = new ZooKeeperGroup<NodeState>(curator, path, NodeState.class);
        group.add(listener);
        members.add(group);
    }
    for (ZooKeeperGroup group : members) {
        assertFalse(group.isConnected());
        assertFalse(group.isMaster());
    }
    NIOServerCnxnFactory cnxnFactory = startZooKeeper(port);
    curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    // first to start should be master if members are ordered...
    int i = 0;
    for (ZooKeeperGroup group : members) {
        group.start();
        group.update(new NodeState("foo" + i));
        i++;
        // wait for registration
        while (group.getId() == null) {
            TimeUnit.MILLISECONDS.sleep(100);
        }
    }
    boolean firsStartedIsMaster = members.get(0).isMaster();
    for (ZooKeeperGroup group : members) {
        group.close();
    }
    curator.close();
    cnxnFactory.shutdown();
    cnxnFactory.join();
    assertTrue("first started is master", firsStartedIsMaster);
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) ArrayList(java.util.ArrayList) ZooKeeperGroup(io.fabric8.groups.internal.ZooKeeperGroup) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)27 Test (org.junit.Test)25 File (java.io.File)18 HashMap (java.util.HashMap)16 Map (java.util.Map)10 Git (org.eclipse.jgit.api.Git)10 ArrayList (java.util.ArrayList)9 GitPatchRepository (io.fabric8.patch.management.impl.GitPatchRepository)8 Bundle (org.osgi.framework.Bundle)8 PatchException (io.fabric8.patch.management.PatchException)7 URISyntaxException (java.net.URISyntaxException)7 BundleException (org.osgi.framework.BundleException)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 CuratorFramework (org.apache.curator.framework.CuratorFramework)6 Container (io.fabric8.api.Container)5 CreateContainerMetadata (io.fabric8.api.CreateContainerMetadata)5 FabricService (io.fabric8.api.FabricService)5 GitPatchManagementServiceImpl (io.fabric8.patch.management.impl.GitPatchManagementServiceImpl)5 HashSet (java.util.HashSet)5 ObjectId (org.eclipse.jgit.lib.ObjectId)5