Beispiel #1
0
        /// <summary>
        /// Handles results of async puts of pieces.
        /// </summary>
        /// <remarks>Fails on the first failed piece.</remarks>
        public void OnDhtPutReturns(IAsyncResult result)
        {
            AsyncResult              ar           = (AsyncResult)result;
            AsyncFragsOpState        apfs         = (AsyncFragsOpState)ar.AsyncState;
            BrunetDhtPutOp           op           = (BrunetDhtPutOp)ar.AsyncDelegate;
            AsyncPutFragsGlobalState global_state =
                (AsyncPutFragsGlobalState)apfs.GlobalState;
            AsyncOpState piece_state = apfs.PieceState;

            bool succ = op.EndInvoke(ar);

            lock (global_state.SyncRoot) {
                if (succ)
                {
                    Logger.WriteLineIf(LogLevel.Verbose, _log_props,
                                       string.Format("Put succeeded for piece: {0}",
                                                     Encoding.UTF8.GetString(piece_state.Key)));

                    global_state.OpSuccCount++;
                    if (global_state.OpSuccCount == global_state.ExpectedPositiveReturnNum)
                    {
                        Logger.WriteLineIf(LogLevel.Verbose, _log_props,
                                           string.Format("All pieces of {0} successfully put. Firing PutStoppedEvent",
                                                         Encoding.UTF8.GetString(global_state.BaseKey)));
                        global_state.Returns.Enqueue(new PutFragsStoppedEventArgs(null));
                    }
                    if ((global_state.Concurrency > 1 && global_state.OpSuccCount %
                         global_state.Concurrency == 0) || global_state.OpSuccCount ==
                        global_state.ExpectedPositiveReturnNum)
                    {
                        // put concurrently and a batch finishes.
                        global_state.BatchFinishedEvent.Set();
                    }
                }
                else
                {
                    Logger.WriteLineIf(LogLevel.Verbose, _log_props,
                                       string.Format("Put failed for piece: {0}. Firing PutStoppedEvent",
                                                     Encoding.UTF8.GetString(piece_state.Key)));
                    //No retry at this level currently, stop put operation.
                    global_state.Returns.Enqueue(new PutFragsStoppedEventArgs(piece_state));
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Concurrently puts pieces into DHT
        /// </summary>
        /// <returns>True if successful</returns>
        private bool PutFragsConcurrently(FragmentationInfo fragInfo,
                                          byte[] infoKey, int ttl, IList <FingerprintedData> fragments)
        {
            bool ret;
            //prepare the global state
            int                      max_enqueues = 1; //we only need a true/false answer
            BlockingQueue            bq_result    = new BlockingQueue(max_enqueues);
            AsyncPutFragsGlobalState global_state =
                new AsyncPutFragsGlobalState(fragments.Count, fragInfo.BaseKey, bq_result);
            //@TODO tweak the number of concurrency_degree here.
            int concurrency_degree = Concurrency;

            global_state.Concurrency = concurrency_degree;
            //Put pieces
            for (int index = 0; index < fragments.Count; index++)
            {
                FingerprintedData fpd           = fragments[index];
                byte[]            serializedFpd = fpd.SerializeTo();
                Logger.WriteLineIf(LogLevel.Verbose, _log_props,
                                   string.Format("Size after serialization {0}", serializedFpd.Length));
                byte[] piece_key        = BuildFragmentKey(fragInfo.BaseKey, index);
                string piece_key_string = Encoding.UTF8.GetString(piece_key);
                //piece state
                AsyncOpState      aps  = new AsyncOpState(piece_key, serializedFpd, ttl);
                AsyncFragsOpState apfs = new AsyncFragsOpState(global_state, aps);
                //async put, one instance of IDht per put because thread safty not guaranteed
                IDht proxy = DhtServiceClient.GetXmlRpcDhtClient(_svc_uri.Port);
                BrunetDhtClientOperations brdht_ops = new BrunetDhtClientOperations(
                    proxy);
                // Fire the async XML-RPC call.
                lock (global_state.SyncRoot) {
                    brdht_ops.BeginPutWithCallback(piece_key, serializedFpd, ttl,
                                                   new AsyncCallback(this.OnDhtPutReturns), apfs);
                }

                Logger.WriteLineIf(LogLevel.Verbose, _log_props,
                                   string.Format("Put piece {0} to DHT began (asynchronously)",
                                                 piece_key_string));

                if ((concurrency_degree > 1 && (index + 1) % concurrency_degree == 0) ||
                    index == fragments.Count - 1)
                {
                    // Stop to wait for batch finish or all finish
                    global_state.BatchFinishedEvent.WaitOne();
                    if (concurrency_degree > 1)
                    {
                        Logger.WriteLineIf(LogLevel.Verbose, _log_props,
                                           string.Format("Batch {0} finished. Moving on",
                                                         (int)((index + 1) / concurrency_degree)));
                    }
                }
            }

            //block here until result comes
            PutFragsStoppedEventArgs stop_args =
                (PutFragsStoppedEventArgs)bq_result.Dequeue();

            // Deal with info object.
            if (stop_args.IsSuccessful)
            {
                ret = _dht.Put(infoKey, fragInfo.SerializeTo(), ttl);
                if (ret)
                {
                    Logger.WriteLineIf(LogLevel.Verbose, _log_props,
                                       string.Format("FragmentationInfo {0} successfully put",
                                                     Encoding.UTF8.GetString(infoKey)));
                }
            }
            else
            {
                ret = false;
            }

            return(ret);
        }
Beispiel #3
0
        /// <summary>
        /// Concurrently puts pieces into DHT
        /// </summary>
        /// <returns>True if successful</returns>
        private bool PutFragsConcurrently(FragmentationInfo fragInfo,
            byte[] infoKey, int ttl, IList<FingerprintedData> fragments)
        {
            bool ret;
              //prepare the global state
              int max_enqueues = 1; //we only need a true/false answer
              BlockingQueue bq_result = new BlockingQueue(max_enqueues);
              AsyncPutFragsGlobalState global_state =
              new AsyncPutFragsGlobalState(fragments.Count, fragInfo.BaseKey, bq_result);
              //@TODO tweak the number of concurrency_degree here.
              int concurrency_degree = Concurrency;
              global_state.Concurrency = concurrency_degree;
              //Put pieces
              for (int index = 0; index < fragments.Count; index++) {
            FingerprintedData fpd = fragments[index];
            byte[] serializedFpd = fpd.SerializeTo();
            Logger.WriteLineIf(LogLevel.Verbose, _log_props,
            string.Format("Size after serialization {0}", serializedFpd.Length));
            byte[] piece_key = BuildFragmentKey(fragInfo.BaseKey, index);
            string piece_key_string = Encoding.UTF8.GetString(piece_key);
            //piece state
            AsyncOpState aps = new AsyncOpState(piece_key, serializedFpd, ttl);
            AsyncFragsOpState apfs = new AsyncFragsOpState(global_state, aps);
            //async put, one instance of IDht per put because thread safty not guaranteed
            IDht proxy = DhtServiceClient.GetXmlRpcDhtClient(_svc_uri.Port);
            BrunetDhtClientOperations brdht_ops = new BrunetDhtClientOperations(
              proxy);
            // Fire the async XML-RPC call.
            lock (global_state.SyncRoot) {
              brdht_ops.BeginPutWithCallback(piece_key, serializedFpd, ttl,
              new AsyncCallback(this.OnDhtPutReturns), apfs);
            }

            Logger.WriteLineIf(LogLevel.Verbose, _log_props,
              string.Format("Put piece {0} to DHT began (asynchronously)",
              piece_key_string));

            if ((concurrency_degree > 1 && (index + 1) % concurrency_degree == 0) ||
              index == fragments.Count - 1) {
              // Stop to wait for batch finish or all finish
              global_state.BatchFinishedEvent.WaitOne();
              if (concurrency_degree > 1) {
            Logger.WriteLineIf(LogLevel.Verbose, _log_props,
              string.Format("Batch {0} finished. Moving on",
              (int)((index + 1) / concurrency_degree)));
              }
            }
              }

              //block here until result comes
              PutFragsStoppedEventArgs stop_args =
            (PutFragsStoppedEventArgs)bq_result.Dequeue();

              // Deal with info object.
              if (stop_args.IsSuccessful) {
            ret = _dht.Put(infoKey, fragInfo.SerializeTo(), ttl);
            if (ret) {
              Logger.WriteLineIf(LogLevel.Verbose, _log_props,
            string.Format("FragmentationInfo {0} successfully put",
            Encoding.UTF8.GetString(infoKey)));
            }
              } else {
            ret = false;
              }

              return ret;
        }