Exemplo n.º 1
0
        /// <summary>
        /// Inicializa a instancia.
        /// </summary>
        /// <returns></returns>
        internal virtual bool Initialize()
        {
            bool flag = false;

            if (_props != null)
            {
                if (_props.Contains("index-for-all"))
                {
                    _indexForAll = Convert.ToBoolean(_props["index-for-all"]);
                    flag         = _indexForAll;
                }
                if (_props.Contains("index-classes"))
                {
                    Hashtable indexClasses = _props["index-classes"] as Hashtable;
                    _typeMap = new TypeInfoMap(indexClasses);
                    IDictionaryEnumerator enumerator = indexClasses.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        Hashtable hashtable2 = enumerator.Value as Hashtable;
                        string    type       = "";
                        if (hashtable2 != null)
                        {
                            type = (string)hashtable2["id"];
                            var attributes = new List <string>();
                            IDictionaryEnumerator enumerator2 = hashtable2.GetEnumerator();
                            while (enumerator2.MoveNext())
                            {
                                Hashtable hashtable3 = enumerator2.Value as Hashtable;
                                if (hashtable3 != null)
                                {
                                    IDictionaryEnumerator enumerator3 = hashtable3.GetEnumerator();
                                    while (enumerator3.MoveNext())
                                    {
                                        Hashtable hashtable4 = enumerator3.Value as Hashtable;
                                        if (hashtable4 != null)
                                        {
                                            attributes.Add(hashtable4["id"] as string);
                                        }
                                    }
                                }
                            }
                            if (attributes.Count > 0)
                            {
                                _indexMap[type] = new AttributeIndex(attributes, _cacheName);
                            }
                            else
                            {
                                _indexMap[type] = new TypeIndex(type, _indexForAll);
                            }
                            flag = true;
                        }
                    }
                }
                else
                {
                    _typeMap = new TypeInfoMap(new Hashtable());
                }
            }
            else
            {
                _indexMap["default"] = new VirtualQueryIndex(_cache);
                _typeMap             = new TypeInfoMap(new Hashtable());
            }
            _typeMap.HandleAdded += new Action <int>(TypeMap_HandleAdded);
            if (flag)
            {
                _asyncProcessor = new AsyncProcessor("Cache.QueryIndexManager", _cache.Context.Logger);
                _asyncProcessor.Start();
            }
            return(flag);
        }
Exemplo n.º 2
0
        internal virtual bool Initialize()
        {
            bool indexedDefined = false;

            if (_props != null)
            {
                if (_props.Contains("index-for-all"))
                {
                    _indexForAll   = Convert.ToBoolean(_props["index-for-all"]);
                    indexedDefined = _indexForAll;
                }

                if (_props.Contains("index-classes"))
                {
                    Hashtable indexClasses = _props["index-classes"] as Hashtable;
                    _typeMap = new TypeInfoMap(indexClasses);

                    IDictionaryEnumerator ie = indexClasses.GetEnumerator();
                    while (ie.MoveNext())
                    {
                        Hashtable innerProps = ie.Value as Hashtable;
                        string    typename   = "";

                        if (innerProps != null)
                        {
                            typename = (string)innerProps["id"];
                            ArrayList             attribList = new ArrayList();
                            IDictionaryEnumerator en         = innerProps.GetEnumerator();
                            while (en.MoveNext())
                            {
                                Hashtable attribs = en.Value as Hashtable;
                                if (attribs != null)
                                {
                                    IDictionaryEnumerator ide = attribs.GetEnumerator();
                                    while (ide.MoveNext())
                                    {
                                        Hashtable attrib = ide.Value as Hashtable;
                                        if (attrib != null)
                                        {
                                            attribList.Add(attrib["id"] as string);
                                        }
                                    }
                                }
                            }

                            //attrib level index.
                            if (attribList.Count > 0)
                            {
                                _indexMap[typename] = new AttributeIndex(attribList, _cacheName, typename, _typeMap);
                            }
                            //just a key level index.
                            else
                            {
                                _indexMap[typename] = new TypeIndex(typename, _indexForAll);
                            }
                            indexedDefined = true;
                        }
                    }
                }
            }
            else
            {
                _indexMap["default"] = new VirtualQueryIndex(_cache);
            }
            if (indexedDefined)
            {
                _asyncProcessor = new AsyncProcessor(_cache.Context.NCacheLog);
                _asyncProcessor.Start();
            }
            return(indexedDefined);
        }
Exemplo n.º 3
0
        async static Task Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("usage: .. <bootstrap servers>");
                Environment.Exit(1);
            }

            var brokerAddress = args[0];

            var simulatedWeblogTopic = "simulated-weblog";
            var filteredWeblogTopic  = "filtered-weblog";

            var topicSpecs = new Dictionary <string, TopicSpecification>
            {
                { simulatedWeblogTopic, new TopicSpecification {
                      Name = simulatedWeblogTopic, NumPartitions = 24, ReplicationFactor = 1
                  } },
                { filteredWeblogTopic, new TopicSpecification {
                      Name = filteredWeblogTopic, NumPartitions = 24, ReplicationFactor = 1
                  } }
            };

            CancellationTokenSource cts = new CancellationTokenSource();

            Console.CancelKeyPress += (_, e) => {
                e.Cancel = true; // prevent the process from terminating.
                cts.Cancel();
            };

            await RecreateTopicsAsync(brokerAddress, topicSpecs.Values.ToArray());


            // 1. A processor that generates some fake weblog data.
            Random r = new Random();
            var    fakeDataSourceProcessor = new Processor <Null, Null, Null, string>
            {
                Name             = "fakegen",
                BootstrapServers = brokerAddress,
                OutputTopic      = simulatedWeblogTopic,
                Function         = (_) =>
                {
                    Thread.Sleep(1000);
                    return(new Message <Null, string> {
                        Value = WebLogLine.GenerateFake()
                    });
                }
            };


            // 2. An async processor that does a (mock) geoip lookup, removes pii information
            //    (IP address), and repartitions by country.
            var transformProcessor = new AsyncProcessor <Null, string, string, string>
            {
                Name                  = "geo-lookup-processor",
                BootstrapServers      = brokerAddress,
                InputTopic            = simulatedWeblogTopic,
                OutputTopic           = filteredWeblogTopic,
                OutputOrderPolicy     = OutputOrder.InputOrder,
                ConsumeErrorTolerance = ErrorTolerance.All,
                Function              = async(m) =>
                {
                    try
                    {
                        var logline         = m.Value;
                        var firstSpaceIndex = logline.IndexOf(' ');
                        if (firstSpaceIndex < 0)
                        {
                            throw new FormatException("unexpected logline format");
                        }
                        var ip      = logline.Substring(0, firstSpaceIndex);
                        var country = await MockGeoLookup.GetCountryFromIPAsync(ip);

                        var loglineWithoutIP = logline.Substring(firstSpaceIndex + 1);
                        var dateStart        = loglineWithoutIP.IndexOf('[');
                        var dateEnd          = loglineWithoutIP.IndexOf(']');
                        if (dateStart < 0 || dateEnd < 0 || dateEnd < dateStart)
                        {
                            throw new FormatException("unexpected logline format");
                        }
                        var requestInfo = loglineWithoutIP.Substring(dateEnd + 2);
                        return(new Message <string, string> {
                            Key = country, Value = requestInfo
                        });
                    }
                    catch (Exception)
                    {
                        // Unhandled exceptions in your processing function will cause the
                        // processor to terminate.

                        return(null); // null -> filter (don't write output message corresponding to input message).
                    }
                }
            };

            // 3. A processor that just writes messages to stdout.
            //    Note: Using a Processor would be better here, this
            //    just demonstrates AsyncProcessor can be used as a sink.
            var consoleWriterProcessor = new AsyncProcessor <string, string, Null, Null>
            {
                Name              = "console-writer",
                BootstrapServers  = brokerAddress,
                InputTopic        = filteredWeblogTopic,
                OutputOrderPolicy = OutputOrder.InputOrder,
                Function          = (m) =>
                {
                    Console.WriteLine($"{m.Key} ~~~ {m.Value}");
                    Message <Null, Null> msg = null;
                    return(Task.FromResult(msg)); // don't write anything to output topic.
                }
            };

            // start all the processors in their own threads. Note: typically
            // each task would be in it's own process, and these would typically
            // be spread across machines.
            var processorTasks = new List <Task>();

            processorTasks.Add(Task.Run(() => fakeDataSourceProcessor.Start("1", cts.Token)));
            processorTasks.Add(Task.Run(() => transformProcessor.Start("1", cts.Token)));
            processorTasks.Add(Task.Run(() => consoleWriterProcessor.Start("1", cts.Token)));

            var result = await Task.WhenAny(processorTasks);

            if (result.IsFaulted)
            {
                throw result.Exception;
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Inicializa a instancia.
 /// </summary>
 /// <param name="properties">Propriedades de configuração.</param>
 /// <param name="timeout"></param>
 private void Initialize(IDictionary properties, long timeout)
 {
     properties.Require("properties").NotNull();
     try
     {
         if (properties.Contains("read-thru"))
         {
             IDictionary dictionary = (IDictionary)properties["read-thru"];
             string      str        = (string)dictionary["enabled"];
             if (str.ToLower() == "true")
             {
                 IDictionary dictionary2 = (IDictionary)dictionary["read-thru-providers"];
                 if (dictionary2 != null)
                 {
                     IDictionaryEnumerator enumerator = dictionary2.GetEnumerator();
                     while (enumerator.MoveNext())
                     {
                         if (!_readerProivder.ContainsKey(enumerator.Key.ToString().ToLower()))
                         {
                             _readerProivder.Add(enumerator.Key.ToString().ToLower(), new ReadThruProviderManager(_cacheName, dictionary2[enumerator.Key] as Hashtable, _context));
                         }
                     }
                 }
             }
         }
         if (properties.Contains("write-thru"))
         {
             IDictionary dictionary3 = (IDictionary)properties["write-thru"];
             string      str2        = (string)dictionary3["enabled"];
             if (str2.ToLower() == "true")
             {
                 IDictionary dictionary4 = (IDictionary)dictionary3["write-thru-providers"];
                 if (dictionary4 != null)
                 {
                     IDictionaryEnumerator enumerator2 = dictionary4.GetEnumerator();
                     while (enumerator2.MoveNext())
                     {
                         if (!_writerProivder.ContainsKey(enumerator2.Key.ToString().ToLower()))
                         {
                             _writerProivder.Add(enumerator2.Key.ToString().ToLower(), new WriteThruProviderManager(_cacheName, dictionary4[enumerator2.Key] as Hashtable, _context, (int)timeout, enumerator2.Key.ToString()));
                         }
                     }
                 }
             }
         }
         foreach (KeyValuePair <string, WriteThruProviderManager> pair in _writerProivder)
         {
             if (!pair.Value.AsyncWriteEnabled)
             {
                 this.anyWriteThruEnabled = true;
                 break;
             }
         }
         foreach (KeyValuePair <string, WriteThruProviderManager> pair2 in _writerProivder)
         {
             if (pair2.Value.AsyncWriteEnabled)
             {
                 this.anyWriteBehindEnabled = true;
                 break;
             }
         }
         if ((_writerProivder != null) && this.anyWriteBehindEnabled)
         {
             _writeBehindAsyncProcess = new WriteBehindAsyncProcessor(timeout, _context.Logger);
         }
         if (_readerProivder != null)
         {
             _asyncProc = new AsyncProcessor("Cache.DataSourceMgr", _context.Logger);
             _asyncProc.Start();
         }
     }
     catch (ConfigurationException)
     {
         throw;
     }
     catch (Exception exception)
     {
         throw new ConfigurationException("Configuration Error: " + exception.ToString(), exception);
     }
 }