Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DNM] Delegate agency scheduled task run to submit #2825

Open
wants to merge 3 commits into
base: development
Choose a base branch
from

Conversation

lchanmann
Copy link
Collaborator

@lchanmann lchanmann commented Jun 11, 2023

Issues

  • Since the agency scheduler/timer is single-threaded, scheduled tasks can queue up on long running task

Changes

  • Delegate agency scheduled tasks run to submit to prevent the queue up
    • also prevent CompoundContextAgency from running scheduled tasks without going through execute()

Script to reproduce

import foam.nanos.logger.Loggers;

agency = x.get("threadPool");

N = 20;

for ( i = 0; i < N; i++ ) {
  final int j = i;

  agency.schedule(x, new foam.core.ContextAgent() {
    public void execute(foam.core.X x_) {
      try {
        Thread.sleep(3000 - j * 100);
      } catch ( InterruptedException e ) { }
      Loggers.logger(x_, this).debug(new Object[] { "Run task", j });
    }
  }, "agency-schedule-" + j, 4000+j*100);
}

// last
agency.schedule(x, new foam.core.ContextAgent() {
  public void execute(foam.core.X x_) {
    Loggers.logger(x_, this).debug(new Object[] {"Run agency-schedule last task"});
    throw new RuntimeException("last");
  }
}, "agency-schedule-last", 5000);


print("DONE");

- Prevent queuing up on the agency scheduler/timer (since the timer is single-threaded) on threadPool agency
- Prevent CompoundContextAgency from running scheduled tasks without calling execute()
@jlhughes
Copy link
Collaborator

Believe TaskQueue.remove now needs to be handled by the job itself when it completes.

@lchanmann
Copy link
Collaborator Author

lchanmann commented Jun 12, 2023

Yes it's unchanged the TaskQueue.remove ie, TASK_QUEUE.remove(key); resides in the run() method of the job.

@jlhughes
Copy link
Collaborator

Will not submit return immediately?

@lchanmann
Copy link
Collaborator Author

Yes, submit on threadPool returns immediately.

@jlhughes
Copy link
Collaborator

Yes, submit on threadPool returns immediately.

So then, TaskQueue.remove will occur immediately, allowing concurrent same jobs.

, key);
}
};
TASK_QUEUE.put(key, task);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should be before the run(), no guarantee that calling thread continues before the timertask.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants