Ejemplo n.º 1
0
        /*************************************************************************/
        private void WriteTelemetry(WriteTask fnWrite)
        {
            // Write telemetry in background thread
          #if DEBUG
          #else
            Task.Run(() =>
          #endif
            {
                var nLoggers = _aLogs.Count;

                for (var i = 0; i < nLoggers; ++i)
                {
                    var logger = _aLogs[i];

                    // Only write primary telemetry to non-fallback loggers
                    if (!logger.FallbackOnly)
                    {
                        try
                        {
                            fnWrite(logger.Log);
                        }
                        catch (Exception ex)
                        {
                            // Ok that didn't work, write to a fallback log
                            FallbackTelemetry(fnWrite, i + 1, ex);
                        }
                    }
                }
            }
          #if DEBUG
          #else
            );
          #endif
        }
Ejemplo n.º 2
0
 static void test_db_enumblock(string[] words)
 {
     try
     {
         Console.WriteLine("test_db_enumblock");
         using (var snap = db.UseSnapShot())
         {
             Console.WriteLine("now snap height=" + snap.DataHeight);
             var keyfinder = snap.CreateKeyFinder(LightDB.systemtable_block);
             foreach (byte[] key in keyfinder)
             {
                 var longkey = BitConverter.ToUInt64(key);
                 Console.WriteLine("got a key:" + longkey);
                 var data = snap.GetValue(LightDB.systemtable_block, key);
                 var task = WriteTask.FromRaw(data.value);
                 Console.WriteLine("   task count=" + task.items.Count);
                 foreach (var item in task.items)
                 {
                     Console.WriteLine("   item:" + item.op + ":" + item.key.Length + "," + item.value.Length);
                 }
             }
         }
     }
     catch (Exception err)
     {
         Console.WriteLine("error:" + err.Message);
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Shutdown and flush the Slack writer
 /// </summary>
 public void Dispose()
 {
     lock (Lock)
     {
         WriteTask.Wait();
     }
 }
Ejemplo n.º 4
0
            public static WriteTask NewInstance(FABHandlerContext ctx, object msg, TaskCompletionSource promise)
            {
                WriteTask task = Recycler.Take();

                Init(task, ctx, msg, promise);
                return(task);
            }
Ejemplo n.º 5
0
        internal void Enqueue(StreamHandle handle, byte[] array, int offset, int count, Action <Exception> completion)
        {
            Contract.Requires(handle != null);
            Contract.Requires(array != null && array.Length > 0);
            Contract.Requires(offset >= 0 && count > 0);
            Contract.Requires((offset + count) <= array.Length);

            WriteTask request;

            if (!this.requestPool.TryDequeue(out request))
            {
                request = new WriteTask(this.OnCompleted);
            }

            request.CompletionAction = completion;

            try
            {
                request.Prepare(array, offset, count);
                handle.WriteStream(request);
            }
            catch (Exception exception)
            {
                Log.Error($"{nameof(WriteTaskQueue)} {request} write error.", exception);
                request.Release();
                request.CompletionAction = null;
                throw;
            }
        }
Ejemplo n.º 6
0
            public static WriteTask NewInstance(
                IChannelHandlerContext ctx, object msg, TaskCompletionSource promise)
            {
                WriteTask task = Pool.Value.Take();

                task.ctx     = ctx;
                task.msg     = msg;
                task.promise = promise;

                if (EstimateTaskSizeOnSubmit)
                {
                    ChannelOutboundBuffer buffer = ctx.Channel.Unsafe.OutboundBuffer;

                    // Check for null as it may be set to null if the channel is closed already
                    if (buffer != null)
                    {
                        task.size = ((AbstractChannel)ctx.Channel).EstimatorHandle.Size(msg) + WriteTaskOverhead;
                        buffer.IncrementPendingOutboundBytes(task.size);
                    }
                    else
                    {
                        task.size = 0;
                    }
                }
                else
                {
                    task.size = 0;
                }

                return(task);
            }
Ejemplo n.º 7
0
        /// <summary>
        /// Enqueues a write operation for the given <paramref name="data"/>, returning a <see cref="Task"/> for its completion.
        /// </summary>
        public Task WriteAsync(byte[] data, CancellationToken cancellationToken = default)
        {
            EnsureIsNotDisposing();

            var writeTask = new WriteTask(new[] { data }, cancellationToken);

            _buffer.Enqueue(writeTask);
            return(writeTask.Task);
        }
Ejemplo n.º 8
0
            public void Flush()
            {
                if (WriteTask != null && WriteTask.IsCompleted == false)
                {
                    WriteTask.Wait();
                }

                File.AppendAllText(Filename, Buffer.ToString(), Encoding);
            }
Ejemplo n.º 9
0
        /// <summary>
        /// Sends a message to Slack
        /// </summary>
        /// <param name="MessageText"></param>
        public void Write(string MessageText)
        {
            lock (Lock)
            {
                if (!string.IsNullOrEmpty(WebhookUrl))
                {
                    string WebhookSnapshot   = WebhookUrl;
                    string ChannelSnapshot   = Channel;
                    string UsernameSnapshot  = Username;
                    string IconEmojiSnapshot = IconEmoji;

                    WriteTask = WriteTask.ContinueWith(PreviousTask => WriteAsync(WebhookSnapshot, ChannelSnapshot, UsernameSnapshot, IconEmojiSnapshot, MessageText));
                }
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Enqueues write operations for the given <paramref name="dataSequence"/>, returning a <see cref="Task"/> for its completion.
        /// </summary>
        public Task WriteManyAsync(IEnumerable <byte[]> dataSequence, CancellationToken cancellationToken = default)
        {
            EnsureIsNotDisposing();

            var tasks = new List <Task>();

            foreach (var batch in dataSequence.Batch(100))
            {
                var writeTask = new WriteTask(batch.ToArray(), cancellationToken);
                _buffer.Enqueue(writeTask);
                tasks.Add(writeTask.Task);
            }

            return(Task.WhenAll(tasks));
        }
Ejemplo n.º 11
0
Archivo: Log.cs Proyecto: nerai/Nibbler
        private void WriteOrStash(string s, bool indent = true)
        {
            if (indent)
            {
                var rs = new CuttingStringReader(s);
                var sb = new StringBuilder();

                while (rs.RemainingLength > 0)
                {
                    // Detect line breaks
                    if (rs.Eat("\r\n") || rs.Eat("\n"))
                    {
                        sb.Append("\n");
                        _isAtLineStart = true;
                        continue;
                    }

                    // Indentation
                    if (_isAtLineStart)
                    {
                        for (int j = 0; j < _Indent; j++)
                        {
                            sb.Append('\t');
                        }
                        _isAtLineStart = false;
                    }

                    var c = rs.Read();
                    sb.Append(c);
                }
                s = sb.ToString();
            }

            if (_Stash != null)
            {
                _Stash.Append(s);
            }
            else
            {
                var task = new WriteTask(s);
                _WriteQueue.Add(task);
                if (!AllowAsynchronousWriting)
                {
                    task.Done.WaitOne();
                }
            }
        }
Ejemplo n.º 12
0
        void OnCompleted(WriteTask writeTask, Exception error)
        {
            if (writeTask == null)
            {
                return;
            }

            writeTask.CompletionAction?.Invoke(error);
            writeTask.CompletionAction = null;
            if (this.requestPool.TryEnqueue(writeTask))
            {
                return;
            }

            writeTask.Dispose();
            Log.Trace($"{nameof(WriteTaskQueue)} Local write task pool is full Maximum = ({MaximumPoolSize}).");
        }
Ejemplo n.º 13
0
        internal void WriteStream(WriteTask request)
        {
            Contract.Requires(request != null);

            this.Validate();
            try
            {
                NativeMethods.WriteStream(
                    request.InternalHandle,
                    this.InternalHandle,
                    request.Buffer);
            }
            catch (Exception exception)
            {
                Log.Debug($"{this.HandleType} Failed to write data.", exception);
                throw;
            }
        }
Ejemplo n.º 14
0
            public void WriteLine(string line, Encoding encoding)
            {
                //one thread at a time
                lock (LockObject)
                {
                    if (Encoding == null)
                    {
                        Encoding = encoding;
                    }
                    else
                    {
                        //could be a mess otherwise
                        if (Encoding != encoding)
                        {
                            throw new InvalidOperationException($"Multiple encodings detected while writting to file {Filename}");
                        }
                    }

                    //Wait until there are 64kb of data in the buffer before start writting and lock after that
                    //if the write task is working
                    Buffer.AppendLine(line);
                    if (WriteTask?.IsCompleted ?? true)
                    {
                        if (Buffer.Length >= 64 * 1024)
                        {
                            WriteBuffer();
                        }
                        else
                        {
                            //If it's less than 64kb in the buffer, create a task to automatically flush it after 2 seconds
                            CreateFlushTask();
                        }
                    }
                    else
                    {
                        if (Buffer.Length > 64 * 1024)
                        {
                            //lock this thread until buffer has been emptied by writter
                            WriteTask.Wait();
                        }
                    }
                }
            }
Ejemplo n.º 15
0
        public Task InvokeWriteAsync(IChannelHandlerContext ctx, object msg)
        {
            Contract.Requires(msg != null);
            // todo: check for cancellation
            //if (!validatePromise(ctx, promise, false)) {
            //    // promise cancelled
            //    return;
            //}

            if (Executor.InEventLoop)
            {
                return(ChannelHandlerInvokerUtil.InvokeWriteAsyncNow(ctx, msg));
            }
            else
            {
                var promise = new TaskCompletionSource();
                this.SafeExecuteOutbound(WriteTask.NewInstance(ctx, msg, promise), promise, msg);
                return(promise.Task);
            }
        }
Ejemplo n.º 16
0
        static void DBGetBlock(string[] words)
        {
            UInt64 blockid = UInt64.Parse(words[1]);
            var    msg     = client.Post_snapshot_getblock(lastSnapheight.Value, blockid);
            //var msg = client.Post_snapshot_getvalue(lastSnapheight.Value, protocol_Helper.systemtable_block, BitConverter.GetBytes(blockid));
            var v    = DBValue.FromRaw(msg.data);
            var task = WriteTask.FromRaw(v.value);

            Console.WriteLine("got info=" + msg.ToString());
            foreach (var i in task.items)
            {
                Console.WriteLine("item=" + i.ToString());
            }
            if (task.extData != null)
            {
                foreach (var e in task.extData)
                {
                    Console.WriteLine("extdata=" + e.Key + " len=" + e.Value.Length);
                }
            }
        }
Ejemplo n.º 17
0
        /*************************************************************************/
        private void FallbackTelemetry(WriteTask fnWrite, int start, Exception excep)
        {
            var nLoggers = _aLogs.Count;

            // Go through all the loggers after the last one
            for (var j = start; j < nLoggers; ++j)
            {
                var fallBackLogger = _aLogs[j];

                // Find the first fallback logger
                if (fallBackLogger.FallbackOnly)
                {
                    try
                    {
                        // Log the exception from the previously failed log
                        if (fallBackLogger.FallbackAsError)
                        {
                            try
                            {
                                fallBackLogger.Log.WriteError(excep);
                            }
                            catch
                            {
                                // This is bad
                            }
                        }
                        else // or write the original telemetry
                            fnWrite(fallBackLogger.Log);
                    }
                    catch (Exception ex)
                    {
                        // The fallback logger failed, fallback to the next
                        FallbackTelemetry(fnWrite, start + 1, ex);
                    }

                    break;
                }
            }
        }
Ejemplo n.º 18
0
        Task WriteAsync(object msg, bool flush)
        {
            FABHandlerContext next         = this.FindContextOutbound();
            object            m            = this.pipeline.Touch(msg, next);
            IEventExecutor    nextExecutor = next.Executor;

            if (nextExecutor.InEventLoop)
            {
                return(flush
                    ? next.InvokeWriteAndFlushAsync(m)
                    : next.InvokeWriteAsync(m));
            }
            else
            {
                var promise            = new TaskCompletionSource();
                AbstractWriteTask task = flush
                    ? WriteAndFlushTask.NewInstance(next, m, promise)
                    : (AbstractWriteTask)WriteTask.NewInstance(next, m, promise);
                SafeExecuteOutbound(nextExecutor, task, promise, msg);
                return(promise.Task);
            }
        }
Ejemplo n.º 19
0
        static void WriteTable(string[] words)
        {
            if (lasthash == null)
            {
                Console.WriteLine("get block hash first.");
                return;
            }
            WriteTask write = new WriteTask();

            //必须添加上一个块的hash,要不然服务器不会接受的
            write.extData             = new System.Collections.Generic.Dictionary <string, byte[]>();
            write.extData["lasthash"] = lasthash;

            //createtable 123;

            //write.CreateTable(new TableInfo(new byte[] { 0x01, 0x02, 0x03 }, "hello world", "", DBValue.Type.String));

            write.Put(new byte[] { 0x01, 0x02, 0x03 }, "123".ToBytes_UTF8Encode(), DBValue.FromValue(DBValue.Type.String, "balabala"));

            var srcdata = write.ToBytes();


            var wiftest = "L2CmHCqgeNHL1i9XFhTLzUXsdr5LGjag4d56YY98FqEi4j5d83Mv";//对应地址 AdsNmzKPPG7HfmQpacZ4ixbv9XJHJs2ACz 作为服务器配置的writer
            var prikey  = ThinNeo.Helper_NEO.GetPrivateKeyFromWIF(wiftest);
            var pubkey  = ThinNeo.Helper_NEO.GetPublicKey_FromPrivateKey(prikey);
            var address = ThinNeo.Helper_NEO.GetAddress_FromPublicKey(pubkey);

            SignData signdata = SignData.Sign(prikey, srcdata);

            var b = signdata.VerifySign(address, srcdata);

            Console.WriteLine("sign result=" + b);
            var msg = client.Post_write(srcdata, signdata);

            Console.WriteLine("post write result block[" + msg.blockid + "] = " + msg.blockhash.ToString_Hex());
        }
Ejemplo n.º 20
0
        static void ShowDBBlock(string[] words)
        {
            uint blockid = uint.Parse(words[1]);

            using (var snap = storage.maindb.UseSnapShot())
            {
                var blockiddata = BitConverter.GetBytes((UInt64)blockid);
                var blockhash   = snap.GetValue(StorageService.tableID_BlockID2Hash, blockiddata).value.ToString_Hex();
                Console.WriteLine("block:" + blockid + " hash=" + blockhash);

                var value = snap.GetValue(LightDB.systemtable_block, blockiddata);
                if (value != null && value.type != DBValue.Type.Deleted)
                {
                    var task = WriteTask.FromRaw(value.value);

                    Console.WriteLine("block:" + blockid + " len=" + value.value.Length);
                    Console.WriteLine("==blockitems==");
                    foreach (var i in task.items)
                    {
                        Console.WriteLine(i.ToString());
                    }
                    if (task.extData != null)
                    {
                        Console.WriteLine("==blockext==");
                        foreach (var i in task.extData)
                        {
                            Console.WriteLine(i.Key + "=" + i.Value?.ToString_Hex());
                        }
                    }
                }
                else
                {
                    Console.WriteLine("block:" + blockid + " not exist.");
                }
            }
        }
Ejemplo n.º 21
0
        public async Task OnDB_Write(NetMessage msgRecv, byte[] id)
        {
            var msg = NetMessage.Create("_db.write.back");

            msg.Params["_id"] = id;
            try
            {
                var data      = msgRecv.Params["taskdata"];
                var signdata  = msgRecv.Params["signdata"];
                var writetask = WriteTask.FromRaw(data);
                if (writetask.extData?.ContainsKey("lasthash") == true)
                {
                    var lastblockhashRecv = writetask.extData["lasthash"];
                    lock (dblock)
                    {
                        using (var snap = Program.storage.maindb.UseSnapShot())
                        {
                            var blockidlast = BitConverter.GetBytes((UInt64)(snap.DataHeight - 1));

                            byte[] taskhash = Helper.CalcHash256(data);//.Sha256.ComputeHash(data);
                            //不够用,还需要block高度
                            var lasthashFind = snap.GetValue(StorageService.tableID_BlockID2Hash, blockidlast).value;

                            if (Helper.BytesEquals(lastblockhashRecv, lasthashFind))
                            {
                                //数据追加处理
                                Action <WriteTask, byte[], IWriteBatch> afterparser = (_task, _data, _wb) =>
                                {
                                    if (_wb.snapshot.DataHeight != snap.DataHeight)
                                    {
                                        throw new Exception("sync problem,diff snap found.");
                                    }
                                    _wb.Put(StorageService.tableID_BlockID2Hash, snap.DataHeightBuf, DBValue.FromValue(DBValue.Type.Bytes, taskhash));
                                };
                                //写入数据
                                Program.storage.maindb.Write(writetask, afterparser);

                                //进表
                                msg.Params["blockid"]   = snap.DataHeightBuf;
                                msg.Params["blockhash"] = taskhash;
                            }
                            else
                            {
                                msg.Params["_error"] = "block hash is error".ToBytes_UTF8Encode();
                            }
                        }
                    }
                }
                else
                {
                    msg.Params["_error"] = "no last block hash.".ToBytes_UTF8Encode();
                }
            }
            catch (Exception err)
            {
                msg.Params["_error"] = err.Message.ToBytes_UTF8Encode();
            }

            //这个完全可以不要等待呀
            SendToClient(msg);
        }