def _Acquire(self, lease_time): now = int(time.time() * 1e6) expires = int((time.time() + lease_time) * 1e6) # Only latest value latest_value = row_filters.CellsColumnLimitFilter(1) # Match any lease time value > now which means someone else holds a lock # We can't store these as ints, encode to str. current_lease = row_filters.ValueRangeFilter( start_value=utils.SmartStr(now), inclusive_start=False) # aff4:lease family, column = self.store.GetFamilyColumn(self.store.LEASE_ATTRIBUTE) col_filter = row_filters.ColumnRangeFilter( family, start_column=column, end_column=column) # Note filter chains are evaluated in order so there are performance # considerations with which filter to apply first filter_chain = row_filters.RowFilterChain( [col_filter, current_lease, latest_value]) mutate_row = self.store.table.row(self.subject, filter_=filter_chain) # state=False means no lease or it's expired, in this case take the lock. mutate_row.set_cell(family, column, utils.SmartStr(expires), state=False) # Check in review: I think we want to retry the RPC here? Or should we just # raise like we can't get the lock? existing_lock = self.store.CallWithRetry(mutate_row.commit, "write") if existing_lock: raise ExistingLock("Row %s locked." % self.subject) # We have the lock self.expires = expires self.locked = True
def filter_limit_value_range(project_id, instance_id, table_id): client = bigtable.Client(project=project_id, admin=True) instance = client.instance(instance_id) table = instance.table(table_id) rows = table.read_rows( filter_=row_filters.ValueRangeFilter(b"PQ2A.190405", b"PQ2A.190406")) for row in rows: print_row(row)