Exemple #1
0
        public static ClusterInstance NewClusterInstance(InstanceId id, URI uri, Monitors monitors, ClusterConfiguration configuration, int maxSurvivableFailedMembers, LogProvider logging)
        {
            MultiPaxosServerFactory factory = new MultiPaxosServerFactory(configuration, logging, monitors.NewMonitor(typeof(StateMachines.Monitor)));

            ClusterInstanceInput  input  = new ClusterInstanceInput();
            ClusterInstanceOutput output = new ClusterInstanceOutput(uri);

            ObjectStreamFactory objStreamFactory = new ObjectStreamFactory();

            ProverTimeouts timeouts = new ProverTimeouts(uri);

            InMemoryAcceptorInstanceStore acceptorInstances = new InMemoryAcceptorInstanceStore();

            Config config = mock(typeof(Config));

            when(config.Get(ClusterSettings.max_acceptors)).thenReturn(maxSurvivableFailedMembers);

            DelayedDirectExecutor executor = new DelayedDirectExecutor(logging);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext context = new org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext(id, org.neo4j.helpers.collection.Iterables.iterable(new org.neo4j.cluster.protocol.election.ElectionRole(org.neo4j.cluster.protocol.cluster.ClusterConfiguration.COORDINATOR)), new org.neo4j.cluster.protocol.cluster.ClusterConfiguration(configuration.getName(), logging, configuration.getMemberURIs()), executor, logging, objStreamFactory, objStreamFactory, acceptorInstances, timeouts, new org.neo4j.kernel.ha.cluster.DefaultElectionCredentialsProvider(id, new StateVerifierLastTxIdGetter(), new MemberInfoProvider()), config);
            MultiPaxosContext context = new MultiPaxosContext(id, Iterables.iterable(new ElectionRole(ClusterConfiguration.COORDINATOR)), new ClusterConfiguration(configuration.Name, logging, configuration.MemberURIs), executor, logging, objStreamFactory, objStreamFactory, acceptorInstances, timeouts, new DefaultElectionCredentialsProvider(id, new StateVerifierLastTxIdGetter(), new MemberInfoProvider()), config);

            context.ClusterContext.BoundAt = uri;

            SnapshotContext snapshotContext = new SnapshotContext(context.ClusterContext, context.LearnerContext);

            DelayedDirectExecutor taskExecutor = new DelayedDirectExecutor(logging);
            ProtocolServer        ps           = factory.NewProtocolServer(id, input, output, DirectExecutor, taskExecutor, timeouts, context, snapshotContext);

            return(new ClusterInstance(DirectExecutor, logging, factory, ps, context, acceptorInstances, timeouts, input, output, uri));
        }
Exemple #2
0
        public virtual ClusterInstance NewCopy()
        {
            // A very invasive method of cloning a protocol server. Nonetheless, since this is mostly an experiment at this
            // point, it seems we can refactor later on to have a cleaner clone mechanism.
            // Because state machines share state, and are simultaneously conceptually unaware of each other, implementing
            // a clean snapshot mechanism is very hard. I've opted for having a dirty one here in the test code rather
            // than introducing a hack into the runtime code.

            ProverTimeouts timeoutsSnapshot = _timeouts.snapshot();
            InMemoryAcceptorInstanceStore snapshotAcceptorInstances = _acceptorInstanceStore.snapshot();

            ClusterInstanceOutput output = new ClusterInstanceOutput(_uri);
            ClusterInstanceInput  input  = new ClusterInstanceInput();

            DelayedDirectExecutor executor = new DelayedDirectExecutor(_logging);

            ObjectStreamFactory objectStreamFactory = new ObjectStreamFactory();
            MultiPaxosContext   snapshotCtx         = _ctx.snapshot(_logging, timeoutsSnapshot, executor, snapshotAcceptorInstances, objectStreamFactory, objectStreamFactory, new DefaultElectionCredentialsProvider(_server.ServerId, new StateVerifierLastTxIdGetter(), new MemberInfoProvider())
                                                                    );

            IList <StateMachine> snapshotMachines = new List <StateMachine>();

            foreach (StateMachine stateMachine in _server.StateMachines.StateMachines)
            {
                snapshotMachines.Add(SnapshotStateMachine(_logging, snapshotCtx, stateMachine));
            }

            ProtocolServer snapshotProtocolServer = _factory.constructSupportingInfrastructureFor(_server.ServerId, input, output, executor, timeoutsSnapshot, _stateMachineExecutor, snapshotCtx, snapshotMachines.ToArray());

            return(new ClusterInstance(_stateMachineExecutor, _logging, _factory, snapshotProtocolServer, snapshotCtx, snapshotAcceptorInstances, timeoutsSnapshot, input, output, _uri));
        }
        public override void run(string format, string[] args)
        {
            if (0 == args.Length)
            {
                Console.WriteLine(Help);
            }
            else
            {
                format = args[0];
                ObjectStreamFactory streamFactory = getStreamFactory(format);

                string[] formatArgs = new string[args.Length - 1];
                Array.Copy(args, 1, formatArgs, 0, formatArgs.Length);

                string helpString = createHelpString(format, ArgumentParser.createUsage(streamFactory.Parameters));
                if (0 == formatArgs.Length || (1 == formatArgs.Length && "help".Equals(formatArgs[0])))
                {
                    Console.WriteLine(helpString);
                    Environment.Exit(0);
                }

                string errorMessage = ArgumentParser.validateArgumentsLoudly(formatArgs, streamFactory.Parameters);
                if (null != errorMessage)
                {
                    throw new TerminateToolException(1, errorMessage + "\n" + helpString);
                }

                ObjectStream <T> sampleStream = streamFactory.create(formatArgs);

                try
                {
                    object sample;
                    while ((sample = sampleStream.read()) != null)
                    {
                        Console.WriteLine(sample.ToString());
                    }
                }
                catch (IOException e)
                {
                    throw new TerminateToolException(-1, "IO error while converting data : " + e.Message, e);
                }
                finally
                {
                    if (sampleStream != null)
                    {
                        try
                        {
                            sampleStream.close();
                        }
                        catch (IOException)
                        {
                            // sorry that this can fail
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Validates arguments for a format processed by the <code>factory</code>. </summary>
        /// <param name="factory"> a stream factory </param>
        /// <param name="args"> arguments </param>
        protected internal virtual void validateFactoryArgs(ObjectStreamFactory factory, string[] args)
        {
            string errMessage = ArgumentParser.validateArgumentsLoudly(args, factory.Parameters);

            if (null != errMessage)
            {
                throw new TerminateToolException(1, "Format parameters are invalid: " + errMessage + "\n" + "Usage: " + ArgumentParser.createUsage(factory.Parameters));
            }
        }
        /// <summary>
        /// Validates arguments using parameters from <code>argProxyInterface</code> and the parameters of the
        /// <code>format</code>.
        /// </summary>
        /// <param name="args"> arguments </param>
        /// <param name="argProxyInterface"> interface with parameter descriptions </param>
        /// <param name="format"> data format name </param>
        /// @param <A> A </param>
        protected internal virtual void validateAllArgs <A>(string[] args, Type argProxyInterface, string format)
        {
            ObjectStreamFactory factory = getStreamFactory(format);
            string errMessage           = ArgumentParser.validateArgumentsLoudly(args, argProxyInterface, factory.getParameters <A>());

            if (null != errMessage)
            {
                throw new TerminateToolException(1, errMessage + "\n" + getHelp(format));
            }
        }
        public override void run(string format, string[] args)
        {
            validateAllArgs(args, this.paramsClass, format);

            @params = ArgumentParser.parse(ArgumentParser.filter(args, this.paramsClass), this.paramsClass);

            factory = getStreamFactory(format);
            string[] fargs = ArgumentParser.filter(args, factory.Parameters);
            validateFactoryArgs(factory, fargs);
            sampleStream = factory.create(fargs);
        }
        /// <summary>
        /// Returns stream factory for the type of this tool for the </code>format</code>.
        /// </summary>
        /// <param name="format"> data format name </param>
        /// <returns> stream factory for the type of this tool for the format </returns>
        protected internal virtual ObjectStreamFactory getStreamFactory(string format)
        {
            ObjectStreamFactory factory = StreamFactoryRegistry.getFactory(type, format);

            if (null != factory)
            {
                return(factory);
            }
            else
            {
                throw new TerminateToolException(1, "Format " + format + " is not found.\n" + Help);
            }
        }
 public override string getHelp(string format)
 {
     if ("".Equals(format) || StreamFactoryRegistry.DEFAULT_FORMAT.Equals(format))
     {
         return(getBasicHelp(paramsClass, StreamFactoryRegistry.getFactory(type, StreamFactoryRegistry.DEFAULT_FORMAT).getParameters <P>()));
     }
     else
     {
         ObjectStreamFactory factory = StreamFactoryRegistry.getFactory(type, format);
         if (null == factory)
         {
             throw new TerminateToolException(1, "Format " + format + " is not found.\n" + Help);
         }
         return("Usage: " + CLI.CMD + " " + Name + "." + format + " " + ArgumentParser.createUsage(paramsClass, factory.getParameters <P>()));
     }
 }
Exemple #9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testFailure() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void TestFailure()
        {
            ScriptableNetworkFailureLatencyStrategy networkLatency = new ScriptableNetworkFailureLatencyStrategy();
            NetworkMock network = new NetworkMock(NullLogService.Instance, new Monitors(), 50, new MultipleFailureLatencyStrategy(networkLatency), new MessageTimeoutStrategy(new FixedTimeoutStrategy(1000)));

            IList <TestProtocolServer> nodes = new List <TestProtocolServer>();

            TestProtocolServer server = network.AddServer(1, URI.create("cluster://server1"));

            server.NewClient(typeof(Cluster)).create("default");
            network.TickUntilDone();
            nodes.Add(server);

            for (int i = 1; i < 3; i++)
            {
                TestProtocolServer protocolServer = network.AddServer(i + 1, new URI("cluster://server" + (i + 1)));
                protocolServer.NewClient(typeof(Cluster)).join("default", new URI("cluster://server1"));
                network.Tick(10);
                nodes.Add(protocolServer);
            }

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcast atomicBroadcast = nodes.get(0).newClient(org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcast.class);
            AtomicBroadcast     atomicBroadcast     = nodes[0].NewClient(typeof(AtomicBroadcast));
            ObjectStreamFactory objectStreamFactory = new ObjectStreamFactory();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer serializer = new org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer(objectStreamFactory, objectStreamFactory);
            AtomicBroadcastSerializer serializer = new AtomicBroadcastSerializer(objectStreamFactory, objectStreamFactory);

            atomicBroadcast.Broadcast(serializer.Broadcast(new DaPayload()));

            networkLatency.NodeIsDown("cluster://server2");
            networkLatency.NodeIsDown("cluster://server3");

            atomicBroadcast.Broadcast(serializer.Broadcast(new DaPayload()));
            network.Tick(100);
            networkLatency.NodeIsUp("cluster://server3");
            network.Tick(1000);

            foreach (TestProtocolServer node in nodes)
            {
                node.NewClient(typeof(Cluster)).leave();
                network.Tick(10);
            }
        }
Exemple #10
0
//JAVA TO C# CONVERTER WARNING: 'final' parameters are ignored unless the option to convert to C# 7.2 'in' parameters is selected:
//ORIGINAL LINE: public ClusterClientModule(org.neo4j.kernel.lifecycle.LifeSupport life, org.neo4j.kernel.impl.util.Dependencies dependencies, final org.neo4j.kernel.monitoring.Monitors monitors, final org.neo4j.kernel.configuration.Config config, org.neo4j.logging.internal.LogService logService, org.neo4j.cluster.protocol.election.ElectionCredentialsProvider electionCredentialsProvider)
        public ClusterClientModule(LifeSupport life, Dependencies dependencies, Monitors monitors, Config config, LogService logService, ElectionCredentialsProvider electionCredentialsProvider)
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.logging.LogProvider logging = org.neo4j.cluster.logging.AsyncLogging.provider(life, logService.getInternalLogProvider());
            LogProvider logging = AsyncLogging.provider(life, logService.InternalLogProvider);

            InternalLoggerFactory.DefaultFactory = new NettyLoggerFactory(logging);

            TimeoutStrategy timeoutStrategy = (new MessageTimeoutStrategy(new FixedTimeoutStrategy(config.Get(ClusterSettings.default_timeout).toMillis()))).timeout(HeartbeatMessage.sendHeartbeat, config.Get(ClusterSettings.heartbeat_interval).toMillis()).timeout(HeartbeatMessage.timed_out, config.Get(ClusterSettings.heartbeat_timeout).toMillis()).timeout(AtomicBroadcastMessage.broadcastTimeout, config.Get(ClusterSettings.broadcast_timeout).toMillis()).timeout(LearnerMessage.learnTimedout, config.Get(ClusterSettings.learn_timeout).toMillis()).timeout(ProposerMessage.phase1Timeout, config.Get(ClusterSettings.phase1_timeout).toMillis()).timeout(ProposerMessage.phase2Timeout, config.Get(ClusterSettings.phase2_timeout).toMillis()).timeout(ClusterMessage.joiningTimeout, config.Get(ClusterSettings.join_timeout).toMillis()).timeout(ClusterMessage.configurationTimeout, config.Get(ClusterSettings.configuration_timeout).toMillis()).timeout(ClusterMessage.leaveTimedout, config.Get(ClusterSettings.leave_timeout).toMillis()).timeout(ElectionMessage.electionTimeout, config.Get(ClusterSettings.election_timeout).toMillis());

            MultiPaxosServerFactory protocolServerFactory = new MultiPaxosServerFactory(new ClusterConfiguration(config.Get(ClusterSettings.cluster_name), logging), logging, monitors.NewMonitor(typeof(StateMachines.Monitor)));

            NetworkReceiver receiver = dependencies.satisfyDependency(new NetworkReceiver(monitors.NewMonitor(typeof(NetworkReceiver.Monitor)), new ConfigurationAnonymousInnerClass(this, config)
                                                                                          , logging));

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory objectInputStreamFactory = new org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory();
            ObjectInputStreamFactory objectInputStreamFactory = new ObjectStreamFactory();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory objectOutputStreamFactory = new org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory();
            ObjectOutputStreamFactory objectOutputStreamFactory = new ObjectStreamFactory();

            receiver.AddNetworkChannelsListener(new NetworkChannelsListenerAnonymousInnerClass(this, logging, objectInputStreamFactory, objectOutputStreamFactory));

            NetworkSender sender = dependencies.satisfyDependency(new NetworkSender(monitors.NewMonitor(typeof(NetworkSender.Monitor)), new ConfigurationAnonymousInnerClass(this, config)
                                                                                    , receiver, logging));

            ExecutorLifecycleAdapter stateMachineExecutor = new ExecutorLifecycleAdapter(() => Executors.newSingleThreadExecutor(new NamedThreadFactory("State machine", monitors.NewMonitor(typeof(NamedThreadFactory.Monitor)))));

            AcceptorInstanceStore acceptorInstanceStore = new InMemoryAcceptorInstanceStore();

            _server = protocolServerFactory.NewProtocolServer(config.Get(ClusterSettings.server_id), timeoutStrategy, receiver, sender, acceptorInstanceStore, electionCredentialsProvider, stateMachineExecutor, objectInputStreamFactory, objectOutputStreamFactory, config);

            life.Add(sender);
            life.Add(stateMachineExecutor);
            life.Add(receiver);

            // Timeout timer - triggers every 10 ms
            life.Add(new TimeoutTrigger(_server, monitors));

            life.add(new ClusterJoin(new ConfigurationAnonymousInnerClass(this, config)
                                     , _server, logService));

            ClusterClient = dependencies.SatisfyDependency(new ClusterClient(life, _server));
        }
Exemple #11
0
        /// <summary>
        /// Returns a factory which reads format named <param>formatName</param> and
        /// instantiates streams producing objects of <param>sampleClass</param> class.
        /// </summary>
        /// <param name="sampleClass"> class of the objects, produced by the streams instantiated by the factory </param>
        /// <param name="formatName">  name of the format, if null, assumes OpenNLP format </param>
        /// <returns> factory instance </returns>

        public static ObjectStreamFactory getFactory(Type sampleClass, string formatName)
        {
            if (null == formatName)
            {
                formatName = DEFAULT_FORMAT;
            }

            ObjectStreamFactory factory = registry.ContainsKey(sampleClass) ? registry[sampleClass][formatName] : null;

            if (factory != null)
            {
                return(factory);
            }
            else
            {
                try
                {
                    Type factoryClazz = Type.GetType(formatName);

                    // TODO: Need to check if it can produce the desired output
                    // Otherwise there will be class cast exceptions later in the flow

                    try
                    {
                        if (factoryClazz != null)
                        {
                            return((ObjectStreamFactory)Activator.CreateInstance(factoryClazz));
                        }
                    }
                    catch (InstantiationException)
                    {
                        return(null);
                    }
                    catch (IllegalAccessException)
                    {
                        return(null);
                    }
                }
                catch (ClassNotFoundException)
                {
                    return(null);
                }
            }
        }
Exemple #12
0
        /// <summary>
        /// Registers <param>factory</param> which reads format named <param>formatName</param> and
        /// instantiates streams producing objects of <param>sampleClass</param> class.
        /// </summary>
        /// <param name="sampleClass"> class of the objects, produced by the streams instantiated by the factory </param>
        /// <param name="formatName">  name of the format </param>
        /// <param name="factory">     instance of the factory </param>
        /// <returns> true if the factory was successfully registered </returns>
        public static bool registerFactory(Type sampleClass, string formatName, ObjectStreamFactory factory)
        {
            bool result;
            IDictionary <string, ObjectStreamFactory> formats = registry[sampleClass];

            if (null == formats)
            {
                formats = new Dictionary <string, ObjectStreamFactory>();
            }
            if (!formats.ContainsKey(formatName))
            {
                formats[formatName]   = factory;
                registry[sampleClass] = formats;
                result = true;
            }
            else
            {
                result = false;
            }
            return(result);
        }
Exemple #13
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldDeepClone()
        public virtual void ShouldDeepClone()
        {
            // Given
            ObjectStreamFactory   objStream                 = new ObjectStreamFactory();
            AcceptorInstanceStore acceptorInstances         = mock(typeof(AcceptorInstanceStore));
            Executor                    executor            = mock(typeof(Executor));
            Timeouts                    timeouts            = mock(typeof(Timeouts));
            ClusterConfiguration        clusterConfig       = new ClusterConfiguration("myCluster", NullLogProvider.Instance);
            ElectionCredentialsProvider electionCredentials = mock(typeof(ElectionCredentialsProvider));

            Config config = mock(typeof(Config));

            when(config.Get(ClusterSettings.max_acceptors)).thenReturn(10);

            MultiPaxosContext ctx = new MultiPaxosContext(new InstanceId(1), Collections.emptyList(), clusterConfig, executor, NullLogProvider.Instance, objStream, objStream, acceptorInstances, timeouts, electionCredentials, config);

            // When
            MultiPaxosContext snapshot = ctx.Snapshot(NullLogProvider.Instance, timeouts, executor, acceptorInstances, objStream, objStream, electionCredentials);

            // Then
            assertEquals(ctx, snapshot);
        }
Exemple #14
0
        public sealed override void run(string format, string[] args)
        {
            ModelUpdaterParams @params = validateAndParseParams <ModelUpdaterParams>(ArgumentParser.filter(args, typeof(ModelUpdaterParams)), typeof(ModelUpdaterParams));

            // Load model to be updated
            Jfile       modelFile           = @params.Model;
            ParserModel originalParserModel = (new ParserModelLoader()).load(modelFile);

            ObjectStreamFactory factory = getStreamFactory(format);

            string[] fargs = ArgumentParser.filter(args, factory.Parameters);
            validateFactoryArgs(factory, fargs);
            ObjectStream <Parse> sampleStream = factory.create <Parse>(fargs);

            ParserModel updatedParserModel;

            try
            {
                updatedParserModel = trainAndUpdate(originalParserModel, sampleStream, @params);
            }
            catch (IOException e)
            {
                throw new TerminateToolException(-1, "IO error while reading training data or indexing data: " + e.Message, e);
            }
            finally
            {
                try
                {
                    sampleStream.close();
                }
                catch (IOException)
                {
                    // sorry that this can fail
                }
            }

            CmdLineUtil.writeModel("parser", modelFile, updatedParserModel);
        }