c# - Azure Service Fabric Scaling -
i have app runs scheduled tasks on azure service fabric. app must run thirty forty tasks @ same time, using asynchronous programming. have questions:
do recommend running tasks asynchronously? if not, should run task synchronously , scale up? how scale up? need no return information running task.
should separate queuing , dequeuing separate stateful services? if so, how these services communicate each other?
here code:
internal sealed class jmataskrunner : statefulservice { public jmataskrunner(statefulservicecontext context) : base(context) { } /// <summary> /// optional override create listeners (e.g., http, service remoting, wcf, etc.) service replica handle client or user requests. /// </summary> /// <remarks> /// more information on service communication, see http://aka.ms/servicefabricservicecommunication /// </remarks> /// <returns>a collection of listeners.</returns> protected override ienumerable<servicereplicalistener> createservicereplicalisteners() { return new servicereplicalistener[0]; } public async task<list<jmatask>> getmessagesasync() { await addtasks(); list<jmatask> ts = new list<jmatask>(); ireliablequeue<jmatask> tasks = await this.statemanager.getoraddasync<ireliablequeue<jmatask>>("jmatasks"); using (itransaction tx = this.statemanager.createtransaction()) { var messagesenumerable = await tasks.createenumerableasync(tx); using (var enumerator = messagesenumerable.getasyncenumerator()) { while (await enumerator.movenextasync(cancellationtoken.none)) { ts.add(enumerator.current); } } } return ts; //return messagesenumerable.tolist(); } public async task addmessageasync(jmatask task) { ireliablequeue<jmatask> tasks = await this.statemanager.getoraddasync<ireliablequeue<jmatask>>("jmatasks"); using (itransaction tx = this.statemanager.createtransaction()) { await tasks.enqueueasync(tx, task); await tx.commitasync(); } } /// <summary> /// main entry point service replica. /// method executes when replica of service becomes primary , has write status. /// </summary> /// <param name="cancellationtoken">canceled when service fabric needs shut down service replica.</param> protected override async task runasync(cancellationtoken cancellationtoken) { // todo: replace following sample code own logic // or remove runasync override if it's not needed in service. ireliablequeue<jmatask> tasks = await this.statemanager.getoraddasync<ireliablequeue<jmatask>>("jmatasks"); while (true) { cancellationtoken.throwifcancellationrequested(); var messagesenumerable = await getmessagesasync(); using (itransaction tx = this.statemanager.createtransaction()) { foreach (var message in messagesenumerable) { var result = await tasks.trydequeueasync(tx); await performtask(result.value); } await tx.commitasync(); await task.delay(timespan.fromseconds(2), cancellationtoken); } } } async task<jmatask> performtask(jmatask task) { await task.run(() => perform(task)); return task; } void perform(jmatask task) { thread.sleep(50000); } async task<jmatask> addtasks() { m_taskprovider = jmataskfactory.get(constring); //list<jmatask> tasks = m_taskprovider.getalltasks(); //foreach(jmatask task in tasks) //{ // await addmessageasync(task); //} jmatask task = m_taskprovider.getjmatask(80); jmatask task2 = m_taskprovider.getjmatask(97); await addmessageasync(task); await addmessageasync(task2); return new jmatask(); } }
for service fabric (and other actor-based systems), typically want scale out opposed scaling up [scaling vs scaling out] see (http://www.vtagion.com/scalability-scale-up-scale-out-care/)
azure article on scaling , down (slight misnomer).
essentially, service fabric takes care of of concerns around failover, load balancing etc.
the documentation on how scale clusters worth read.
the documentation on reliable actors goes "threading" model of system, it's actor-based fundamentally asynchronous.
Comments
Post a Comment