Among them, Qrtz_locks is the quartz cluster implementation synchronization mechanism of the row lock table, its table structure is as follows:
Click (here) to collapse or open
- --qrtz_locks table Structure
- CREATE TABLE ' Qrtz_locks ' (
- ' Lock_name ' varchar (+) not NULL,
- PRIMARY KEY (' Lock_name ')
- ) Engine=innodb DEFAULT Charset=utf8;
- --qrtz_locks Records
- +-----------------+
- | Lock_name |
- +-----------------+
- | calendar_access |
- | job_access |
- | misfire_access |
- | state_access |
- | trigger_access |
- +-----------------+
Note: This table structure has new fields in version 2.2 and is not considered here for the time being.
It can be seen that there are 5 records in Qrtz_locks, representing 5 locks, respectively, to achieve multiple quartz node to the job, Trigger, calendar Access synchronization control.
About the mechanism of row locks:
1, MySQL > Set autocommit=0; Set MySQL to not auto-commit first.
2. Select * from es_locks where lock_name = ' trigger_access ' for update; Thread one can lock this line up with a for update
3. Select * from es_locks where lock_name = ' trigger_access ' for update; Thread Cheng cannot get the lock through the for update, and threads wait.
4, commit; Thread one releases the lock through commit
5,///thread Two can access the data, the thread no longer waits.
So, with this mechanism, only one thread at a time can operate the lock-operate-release lock. If the operation takes too long, it will lead to the main thread waiting between the clusters.
A database row lock is a pessimistic lock that cannot be queried by other threads when the table is locked.
There are several ways to lock a database cluster in the source code:
1, the meaning of the Executeinnonmanagedtxlock method is to manage the transaction, not to let the container management transaction lock method.
Click (here) to collapse or open
- Executeinnonmanagedtxlock (
- String Lockname,
- Transactioncallback<t> txcallback, final transactionvalidator<t> txvalidator)
The value of the three parameter lockname is the above-mentioned trigger_access, which indicates the type to be locked.
Txcallback is the method of locking after the callback.
Txvalidator is a validation method, which is generally null
The function performs a lock first, then calls back the method to be manipulated before unlocking.
Check the source code:
Click (here) to collapse or open
- if (lockname! = null) {
- If we aren ' t using DB locks, then delay getting DB connection
- Until after acquiring the lock since it isn ' t needed.
- if (Getlockhandler (). Requiresconnection ()) {
- conn = Getnonmanagedtxconnection ();
- }
- Transowner = Getlockhandler (). Obtainlock (conn, lockname);
- }
- if (conn = = null) {
- conn = Getnonmanagedtxconnection ();
- }
- Final T result = Txcallback.execute (conn);
- try {
- Commitconnection (conn);
- } catch (Jobpersistenceexception e) {
- Rollbackconnection (conn);
- if (Txvalidator = = NULL | |!retryexecuteinnonmanagedtxlock (lockname, New transactioncallback<boolean> () {
- @Override
- Public Boolean Execute (Connection conn) throws Jobpersistenceexception {
- Return Txvalidator.validate (conn, result);
- }
- })) {
- Throw e;
- }
- }
- Long sigtime = Clearandgetsignalschedulingchangeontxcompletion ();
- if (sigtime! = null && sigtime >= 0) {
- Signalschedulingchangeimmediately (Sigtime);
- }
- return result;
- } catch (Jobpersistenceexception e) {
- Rollbackconnection (conn);
- Throw e;
- } catch (RuntimeException e) {
Rollbackconnection (conn);
throw new Jobpersistenceexception ("Unexpected runtime exception:"
+ E.getmessage (), E);
} finally {
try {
ReleaseLock (Lockname, Transowner);
} finally {
Cleanupconnection (conn);
}
}
2, if not through this callback method of lock, is generally:
Getlockhandler (). Obtainlock
Perform
Commitconnection (conn)
ReleaseLock
Cleanupconnection
Second, the source analysis lock
The current line of code locks only use the state_access and trigger_access two.
1, Trigger_access
First understand an article, through the source code to analyze how the quartz through the lock to achieve the cluster environment, trigger state consistency.
Http://www.360doc.com/content/14/0926/08/15077656_412418636.shtml
You can see that the action of the trigger is done primarily with the main thread stdschedulethread, whether it is to get a trigger within 30S that needs to be triggered, or to trigger the process. Select and UPDATE trigger tables
Will be locked first, then unlocked. If the database resources are more competitive, the lock will affect the performance. Consider placing task information in distributed memory, such as Redis, for processing. The database is only timed to count from the load data on Redis.
Reference: Quartz detailed 2:quartz to the fourth chapter 1th, section 2
Implementations are all in the Jobstoresupport class
Lock type
Lock method
Underlying database operations
Note
Executeinnonmanagedtxlock
Acquirenexttrigger
Selecttriggertoacquire
Selecttrigger
Selectjobdetail
Insertfiredtrigger
Search for trigger that require ignition
Select the trigger you want to perform to join the Fired_trigger table
For execution triggerfired
Selectjobdetail
Selectcalendar
Updatefiredtrigger
Triggerexists Updatetrigger
Ignition Trigger
Modifies the trigger state to an executable state.
Recoverjobs
Updatetriggerstatesfromotherstates
Hasmisfiredtriggersinstate Doupdateofmisfiredtrigger
Selecttriggersforrecoveringjobs
Selecttriggersinstate
Deletefiredtriggers
Re-execution in a non-clustered environment
The trigger of failed and misfired
Retryexecuteinnonmanagedtxlock
Releaseacquiredtrigger
Updatetriggerstatefromotherstate
Deletefiredtrigger
Re-release trigger to the initial state in case of exception.
Triggeredjobcomplete
Selecttriggerstatus
Removetrigger updatetriggerstate
Deletefiredtrigger
Triggers processing after the job task completes.
Obtainlock
Recovermisfiredjobs
Hasmisfiredtriggersinstate Doupdateofmisfiredtrigger
Re-executing misfired's trigger
can be executed at startup or periodically by the misfired thread.
Clusterrecover
Selectinstancesfiredtriggerrecords
Updatetriggerstatesforjobfromotherstate
Storetrigger
Deletefiredtriggers
Selectfiredtriggerrecords
Removetrigger
Deleteschedulerstate
The cluster has node faied, allowing the job to be re-executed.
Executeinlock
The database cluster is equivalent to
Executeinnonmanagedtxlock
Storejobandtrigger
Updatejobdetail Insertjobdetail
Triggerexists
Selectjobdetail
Updatetrigger Inserttrigger
Save job and Trigger configuration
Storejob
Save Job
Removejob
Delete Job
Removejobs
Bulk Delete Job
Removetriggers
Bulk Delete triggers
Storejobsandtriggers
Save job and multiple trigger configurations
Removetrigger
Delete Trigger
Replacetrigger
Replace Trigger
Storecalendar
Save timed date
Removecalendar
Delete a timed date
Clearallschedulingdata
Clear all timing data
Pausetrigger
Stop Trigger
Pausejob
Stop task
Pausejobs
Bulk Stop Task
Resumetrigger
Recovery triggers
Resumejob
Restore tasks
Resumejobs
Batch Recovery tasks
Pausetriggers
Bulk Stop Trigger
Resumetriggers
Batch recovery triggers
Pauseall
Stop all
Resumeall
Restore All
---
2, State_trigger
Implementations are all in the Jobstoresupport class
Lock type
Lock method
Underlying database operations
Note
Obtainlock
Docheckin
Clustercheckin
Determine cluster status
Lock cluster state with Lock_state_access first
Re-use Lock_trigger_access to restore cluster operation
---
Quartz-Database Lock