예제 #1
0
        public void TestRollover()
        {
            _service = new VariableServiceImpl(
                VariableServiceImpl.ROLLOVER_READER_BOUNDARY - 100,
                10000,
                new SchedulingServiceImpl(new TimeSourceServiceImpl()),
                SupportEventAdapterService.Service,
                null);

            String[] variables = "a,b,c,d".Split(',');

            var readers = new VariableReader[variables.Length];

            for (int i = 0; i < variables.Length; i++)
            {
                _service.CreateNewVariable <long>(null, variables[i], false, 100L, null);
                _service.AllocateVariableState(variables[i], EPStatementStartMethodConst.DEFAULT_AGENT_INSTANCE_ID, null, false);
                readers[i] = _service.GetReader(variables[i], EPStatementStartMethodConst.DEFAULT_AGENT_INSTANCE_ID);
            }

            for (int i = 0; i < 1000; i++)
            {
                for (int j = 0; j < variables.Length; j++)
                {
                    _service.Write(readers[j].VariableMetaData.VariableNumber, EPStatementStartMethodConst.DEFAULT_AGENT_INSTANCE_ID, 100L + i);
                    _service.Commit();
                }
                ReadCompare(variables, 100L + i);
            }
        }
예제 #2
0
        private void DoLoop(int loopNumber)
        {
            // Set a mark, there should be no number above that number
            int mark = _variableVersionCoord.SetVersionGetMark();

            int[] indexes = GetIndexesShuffled(_variables.Length, _random, loopNumber);
            _marks[loopNumber] = mark;

            // Perform first read of all variables
            int[] readResults = new int[_variables.Length];
            ReadAll(indexes, readResults, mark);

            // Start a write cycle for the write we are getting an exclusive write lock
            using (_variableService.ReadWriteLock.AcquireWriteLock()) {
                // Write every second of the variables
                for (int i = 0; i < indexes.Length; i++)
                {
                    int    variableNum  = indexes[i];
                    String variableName = _variables[variableNum];

                    if (i % 2 == 0)
                    {
                        int newMark = _variableVersionCoord.IncMark();
                        if (Log.IsDebugEnabled)
                        {
                            Log.Debug(".run Thread {0} at mark {1} write variable '{2}' new value {3}",
                                      Thread.CurrentThread.ManagedThreadId,
                                      mark,
                                      variableName,
                                      newMark);
                        }
                        _variableService.Write(_readers[variableNum].VariableMetaData.VariableNumber, EPStatementStartMethodConst.DEFAULT_AGENT_INSTANCE_ID, newMark);
                    }
                }

                // Commit (apply) the changes and unlock
                _variableService.Commit();
            }

            // Read again and compare to first result
            _results[loopNumber] = new int[_variables.Length];
            ReadAll(indexes, _results[loopNumber], mark);

            // compare first read with second read, written values are NOT visible
            for (int i = 0; i < _variables.Length; i++)
            {
                if (_results[loopNumber][i] != readResults[i])
                {
                    String text = "Error in loop#" + loopNumber +
                                  " comparing a re-read result for variable " + _variables[i] +
                                  " expected " + readResults[i] +
                                  " but was " + _results[loopNumber][i];
                    Assert.Fail(text);
                }
            }
        }
예제 #3
0
        public void TestReadWrite()
        {
            Assert.IsNull(_service.GetReader("a", EPStatementStartMethodConst.DEFAULT_AGENT_INSTANCE_ID));

            _service.CreateNewVariable <long>(null, "a", false, 100L, null);
            _service.AllocateVariableState("a", EPStatementStartMethodConst.DEFAULT_AGENT_INSTANCE_ID, null, false);
            VariableReader reader = _service.GetReader("a", EPStatementStartMethodConst.DEFAULT_AGENT_INSTANCE_ID);

            Assert.AreEqual(typeof(long?), reader.VariableMetaData.VariableType);
            Assert.AreEqual(100L, reader.Value);

            _service.Write(reader.VariableMetaData.VariableNumber, EPStatementStartMethodConst.DEFAULT_AGENT_INSTANCE_ID, 101L);
            _service.Commit();
            Assert.AreEqual(100L, reader.Value);
            _service.SetLocalVersion();
            Assert.AreEqual(101L, reader.Value);

            _service.Write(reader.VariableMetaData.VariableNumber, EPStatementStartMethodConst.DEFAULT_AGENT_INSTANCE_ID, 102L);
            _service.Commit();
            Assert.AreEqual(101L, reader.Value);
            _service.SetLocalVersion();
            Assert.AreEqual(102L, reader.Value);
        }
예제 #4
0
        public void TestReadWrite()
        {
            Assert.IsNull(_service.GetReader("a", 0));

            _service.CreateNewVariable <long>(null, "a", false, 100L, null);
            _service.AllocateVariableState("a", 0, null);
            VariableReader reader = _service.GetReader("a", 0);

            Assert.AreEqual(typeof(long?), reader.VariableMetaData.VariableType);
            Assert.AreEqual(100L, reader.Value);

            _service.Write(reader.VariableMetaData.VariableNumber, 0, 101L);
            _service.Commit();
            Assert.AreEqual(100L, reader.Value);
            _service.SetLocalVersion();
            Assert.AreEqual(101L, reader.Value);

            _service.Write(reader.VariableMetaData.VariableNumber, 0, 102L);
            _service.Commit();
            Assert.AreEqual(101L, reader.Value);
            _service.SetLocalVersion();
            Assert.AreEqual(102L, reader.Value);
        }
        /// <summary>
        /// Write new variable values and commit, evaluating assignment expressions using the given
        /// events per stream.
        /// <para />Populates an optional map of new values if a non-null map is passed.
        /// </summary>
        /// <param name="variableService">variable service</param>
        /// <param name="eventsPerStream">events per stream</param>
        /// <param name="valuesWritten">null or an empty map to populate with written values</param>
        /// <param name="exprEvaluatorContext">expression evaluation context</param>
        public void WriteVariables(VariableService variableService,
                                   EventBean[] eventsPerStream,
                                   IDictionary <String, Object> valuesWritten,
                                   ExprEvaluatorContext exprEvaluatorContext)
        {
            ISet <String> variablesBeansCopied = null;

            if (!_copyMethods.IsEmpty())
            {
                variablesBeansCopied = new HashSet <String>();
            }

            // We obtain a write lock global to the variable space
            // Since expressions can contain variables themselves, these need to be unchangeable for the duration
            // as there could be multiple statements that do "var1 = var1 + 1".
            using (variableService.ReadWriteLock.AcquireWriteLock())
            {
                try
                {
                    variableService.SetLocalVersion();

                    var count = 0;
                    foreach (var assignment in _assignments)
                    {
                        var variableMetaData = _metaData[count];
                        int agentInstanceId  = variableMetaData.ContextPartitionName == null ? EPStatementStartMethodConst.DEFAULT_AGENT_INSTANCE_ID : exprEvaluatorContext.AgentInstanceId;
                        var value            = assignment.Evaluator.Evaluate(
                            new EvaluateParams(eventsPerStream, true, exprEvaluatorContext));

                        if (_writers[count] != null)
                        {
                            var reader = variableService.GetReader(
                                variableMetaData.VariableName, exprEvaluatorContext.AgentInstanceId);
                            var current = (EventBean)reader.Value;
                            if (current == null)
                            {
                                value = null;
                            }
                            else
                            {
                                var writeDesc = _writers[count];
                                var copy      = variablesBeansCopied.Add(writeDesc.VariableName);
                                if (copy)
                                {
                                    var copied = _copyMethods.Get(writeDesc.Type).Copy(current);
                                    current = copied;
                                }
                                variableService.Write(variableMetaData.VariableNumber, agentInstanceId, current);
                                writeDesc.Writer.Write(value, current);
                            }
                        }
                        else if (variableMetaData.EventType != null)
                        {
                            var eventBean = _eventAdapterService.AdapterForType(value, variableMetaData.EventType);
                            variableService.Write(variableMetaData.VariableNumber, agentInstanceId, eventBean);
                        }
                        else
                        {
                            if ((value != null) && (_mustCoerce[count]))
                            {
                                value = CoercerFactory.CoerceBoxed(value, variableMetaData.VariableType);
                            }
                            variableService.Write(variableMetaData.VariableNumber, agentInstanceId, value);
                        }

                        count++;

                        if (valuesWritten != null)
                        {
                            valuesWritten.Put(assignment.VariableName, value);
                        }
                    }

                    variableService.Commit();
                }
                catch (Exception ex)
                {
                    Log.Error("Error evaluating on-set variable expressions: " + ex.Message, ex);
                    variableService.Rollback();
                }
            }
        }