class HiveLease { String user; // user name (for identification purposes) long lease_id; // identifies the current lease String db_name; String table_name; String partition_name; // empty for a non-partitioned table long start_utc; // original start time, doesn't change with extensions long end_utc; // end of current lease char lease_type; // 's' for shared or read, 'x' for exclusive or write } ===== Metastore API ====== /** * Tries to acquire lease for the tables/partitions of given lease type. * For a non-partitioned table, partition name is optional. Otherwise lock * is obtained for the partition. * Does not check that the table or partition actually exists so that locks can * be obtained on table that will be created by this query. * This function is guaranteed to be atomic, lease is acquired for all or none. * Returns a lease_id which must be used for extending and releasing the lease. * On error, an LeaseAlreadyExistsException is thrown. */ long grab_lease(List objs, long lease_duration_sec, long lease_wait_duration_sec) throws LeaseAlreadyExistsException, InvalidArgumentsException; /** * Returns current lease info for tables and partitions */ List list_lease(List) throws InvalidArgumentsException; /** * Extends previously acquired lease. */ String extend_lease(long lease_id, List objs) throws InvalidArgumentsException, ExceedsMaxLeaseTimeException; /** * Drops previously acquired lease. */ void drop_lease(long lease_id, List objs) throws InvalidArgumentsException; /** * Release lease acquired by some other process. Should be used * to prematurely drops leases obtained by dead process. * An empty list releases all leases currently held. */ void force_drop_lease(List objs); ===== HIVE SQL ====== SHOW LEASE * DROP FORCE LEASE * ======= Algorithm ======= 1) grab_lease # check for any existing leases for the given objects # SELECT * FROM LEASE WHERE end_utc >= cur_sys_time() AND (db_name = ? AND table_name = ? AND partition_name = ? AND lease_type = 'w') OR .... # if there is atleast one result for above query, return failure # generate unique sequence id (monotonically increasing with time) which is the lease id # get cur_sys_time of database server which will be start_utc of this lease # add lease rows for each of the given objects # INSERT INTO LEASE (...) VALUES (pchakka, LEASE_ID, db_name, table_name, partition_name, start_utc, start_utc + 1hr, 's') # COMMIT # return lease_id if confirm_lease() returns true 2) confirm_lease # check that this lease_id actually got the lease # SELECT lease_id FROM LEASE WHERE end_utc >= cur_sys_time() AND db_name = ? AND table_name = ? AND partition_name = ? ORDER BY end_utc asc, lease_id asc; # If the returned lease_id is not our lease_id then some one else obtained the lease before we did. force drop all leases and return failure. # return success 3) extend_lease # UPDATE LEASE SET end_utc = cur_sys_time() + 1hr WHERE end_utc > cur_sys_time() AND lease_id = ? # return confirm_lease() 4) drop_lease # DROP FROM LEASE where lease_id = ?