实时架构,迭代应用架构、实时、迭代

由网友(朕要翻你的牌)分享简介:抱歉抽象的问题,但我正在寻找的应用程序,做一些相应的操作周期中的某些类型的样品/建议/文章,以及周期的每个迭代应该公开其结果在一定的时间部分(例如, 10秒)。我的应用程序的外部WCF服务和本地数据库之间的数据同步。在每次迭代的应用程序检索数据传送请求WCF服务的变化,并把变化的数据库,反之亦然。其中一个这种应用最硬的...

抱歉抽象的问题,但我正在寻找的应用程序,做一些相应的操作周期中的某些类型的样品/建议/文章,以及周期的每个迭代应该公开其结果在一定的时间部分(例如, 10秒)。

我的应用程序的外部WCF服务和本地数据库之间的数据同步。在每次迭代的应用程序检索数据传送请求WCF服务的变化,并把变化的数据库,反之亦然。其中一个这种应用最硬的要求就是迭代应该解雇每十秒钟。

因此​​,这里的问题就出现了。我如何能保证迭代将完成为不超过10秒了吗?

我想这类型的应用被称为实时应用(在实时操作系统的满耳)。

这是我们使用随机的行为在连接超时行为DAL组件。因此,数据库操作可能需要更长的时间超过10秒。

下面是一个迭代的估计code:

 秒表S1 =新的秒表();
        s1.Start();
        Parallel.ForEach(Global.config.databases,新ParallelOptions {MaxDegreeOfParallelism = -1},(升)=>
        {
            Console.WriteLine(已启动{0},l.key.name);
            DB DB =新的DB(l.connectionString);

            日期时间lastIterationTS =获取previousIterationTS(l.id);

            ExternalService SERV =新ExternalService(l.id);
            名单< ChangedData> ChangedDataDb = db.GetChangedData(DateTime.Now.AddSeconds((lastIterationTS == DateTime.MinValue)-300:-1 *(DateTime.Now  -  lastIterationTS).Seconds));

            名单<数据> ChangedDataService = serv.GetModifiedData();

                    行动syncDBChanges =新的Action(()=>
                        {
                            //ИзменениявБД
                            的foreach(ChangedData d在ChangedDataDb)
                            {
                                尝试
                                {
                                    // ...
                                    //分析和放大器;同步
                                }
                                赶上(例外五)
                                {
                                    logger.InfoEx(Exception_SyncDatabase,e.ToString());
                                }
                            }
                        }
                    );

                    行动syncService =新的Action(()=>
                    {
                        的foreach(在ChangedDataService数据D)
                        {
                            尝试
                            {
                                // ...
                                //分析和放大器;同步
                            }
                            赶上(例外五)
                            {
                                logger.InfoEx(Exception_SyncService,e.ToString());
                            }
                        }
                    });

                    名单< WaitHandle的>处理=新的名单,其中,WaitHandle的>();
                    IAsyncResult的AR1 = syncDBChanges.BeginInvoke(syncDBChanges.EndInvoke,NULL);
                    IAsyncResult的AR2 = syncService.BeginInvoke(syncService.EndInvoke,NULL);

                    handles.Add(ar1.AsyncWaitHandle);
                    handles.Add(ar2.AsyncWaitHandle);

                    WaitHandle.WaitAll的(handles.ToArray(),(int)的((Global.config.syncModifiedInterval  -  1)* 1000));
                    SetCurrentIterationTS(l.id);
                }
                赶上(例外五)
                {
                    Console.WriteLine(e.Message);
                    logger.InfoEx(Exception_Iteration,e.ToString());
                    继续;
                }
            }
            logger.InfoEx(end_Iteration,IterationContextParams);
        }
        );
        s1.Stop();
        Console.WriteLine(主迭代完成{0} ...,s1.Elapsed);
 
SOA 服务设计 传统车载架构的迭代升级

解决方案

您可以考虑几个选择...

杀死迭代如果超过10秒以上,并希望下一次迭代可以完成的过程。这种方法的问题是,有一个很好的可能性,该迭代的无将完成,因此,该同步过程将不会发生。我建议以下选项...

如果迭代时间超过10秒钟,等待它完成,然后跳到下一次迭代(S)。这样可以确保该过程完成ATLEAST一次。以下是参考一个简单的code样品...

 类更新
{
    定时器定时=新的Timer();
    公共对象StateLock =新的对象();
    公共字符串的国家;

    公共更新程序()
    {
        timer.Elapsed + = timer_Elapsed;
        timer.Interval = 10000;
        timer.AutoReset = TRUE;
        timer.Start();
    }

    无效timer_Elapsed(对象发件人,ElapsedEventArgs E)
    {
        如果(状态!=暗战)
        {
            处理();
        }
    }

    私人无效处理()
    {
        尝试
        {
            锁定(StateLock)
            {
                国家=运行;
            }

            // 处理

            锁定(StateLock)
            {
                状态=;
            }
        }
        抓住
        {
            扔;
        }
    }
}
 

...

 类节目
{
    静态无效的主要(字串[] args)
    {
        更新程序更新=新的更新程序();
        到Console.ReadLine();
    }
}
 

Sorry for abstract question, but I'm looking for some samples/advices/articles on type of applications which does some equivalent operations in cycle, and every iteration of cycle should expose its result in certain portion of time (for instance, 10 seconds).

My application does synchronization of data between external WCF service and local database. In every iteration an application retrieves changes of data passing request to WCF service and puts changes to database and vice versa. One of most hard requirement for this application is that iterations should fire every ten seconds.

So here is the issues arises. How can I guarantee that iteration will finish for no more than 10 seconds?

I guess this type of applications called real-time applications (in maner of real-time OS).

DAL components that we use acts randomly on connection timeout behavior. So DB operations may take longer time than 10 seconds.

Here is the estimated code of one iteration:

        Stopwatch s1 = new Stopwatch();
        s1.Start();
        Parallel.ForEach(Global.config.databases, new ParallelOptions { MaxDegreeOfParallelism = -1 }, (l) =>            
        {
            Console.WriteLine("Started for {0}", l.key.name);                
            DB db = new DB(l.connectionString);

            DateTime lastIterationTS = GetPreviousIterationTS(l.id);

            ExternalService serv = new ExternalService(l.id);
            List<ChangedData> ChangedDataDb = db.GetChangedData(DateTime.Now.AddSeconds((lastIterationTS == DateTime.MinValue) ? -300 : -1 * (DateTime.Now - lastIterationTS).Seconds));

            List<Data> ChangedDataService = serv.GetModifiedData();                

                    Action syncDBChanges = new Action(() =>
                        {
                            // Изменения в БД                                   
                            foreach (ChangedData d in ChangedDataDb)
                            {
                                try
                                {
                                    // ...
                                    // analyzing & syncing
                                }
                                catch (Exception e)
                                {
                                    logger.InfoEx("Exception_SyncDatabase", e.ToString());
                                }
                            }
                        }
                    );

                    Action syncService = new Action(() =>
                    {                            
                        foreach (Data d in ChangedDataService)
                        {
                            try
                            {
                                // ...
                                // analyzing & syncing
                            }
                            catch (Exception e)
                            {
                                logger.InfoEx("Exception_SyncService", e.ToString());
                            }
                        }
                    });

                    List<WaitHandle> handles = new List<WaitHandle>();
                    IAsyncResult ar1 = syncDBChanges.BeginInvoke(syncDBChanges.EndInvoke, null);
                    IAsyncResult ar2 = syncService.BeginInvoke(syncService.EndInvoke, null);

                    handles.Add(ar1.AsyncWaitHandle);
                    handles.Add(ar2.AsyncWaitHandle);

                    WaitHandle.WaitAll(handles.ToArray(), (int)((Global.config.syncModifiedInterval - 1) * 1000));
                    SetCurrentIterationTS(l.id);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    logger.InfoEx("Exception_Iteration", e.ToString());
                    continue;
                }
            }
            logger.InfoEx("end_Iteration", IterationContextParams);
        }
        );
        s1.Stop();
        Console.WriteLine("Main iteration done for {0}...", s1.Elapsed);        

解决方案

You can consider a couple of options...

Kill the iteration if it exceeds more than 10 seconds and hope that the next iteration can complete process. The issue with this approach is that there is a good possibility that the none of the iterations will complete and therefore the synchronization process will never occur. I would recommend the following option...

If the iteration takes more than 10 seconds, wait for it to complete and skip the next iteration(s). This way you ensure the process completes atleast once. The following is a simplified code sample for reference...

class Updater
{
    Timer timer = new Timer();
    public object StateLock = new object();
    public string State;

    public Updater()
    {
        timer.Elapsed += timer_Elapsed;
        timer.Interval = 10000;
        timer.AutoReset = true;
        timer.Start();
    }

    void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        if (State != "Running")
        {
            Process();
        }
    }

    private void Process()
    {
        try
        {
            lock (StateLock)
            {
                State = "Running";
            }

            // Process

            lock (StateLock)
            {
                State = "";
            }
        }
        catch
        {
            throw;
        }
    }
}

...

class Program
{
    static void Main(string[] args)
    {
        Updater updater = new Updater();
        Console.ReadLine();
    }
}

阅读全文

相关推荐

最新文章