-
Notifications
You must be signed in to change notification settings - Fork 190
feat: introduce dedicated bucketing types #8712
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
Open
chaen
wants to merge
4
commits into
DIRACGrid:integration
Choose a base branch
from
chaen:v9.1_feat_dedicatedAccountingAgent
base: integration
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e98af17
feat: introduce DataStore dedicated to specific bucketing types
chaen 9fc2cd0
feat (AccountingDB): cache some of the calls for optimization
chaen fa38473
fix (AccountingDB): reset pending lock time when encountering an error
chaen 4440a4d
fix (AccountingDB): escape column name
chaen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,9 @@ | |
| import threading | ||
| import time | ||
|
|
||
| from cachetools import cachedmethod, LRUCache, TTLCache, cached | ||
|
|
||
|
|
||
| from DIRAC import S_ERROR, S_OK | ||
| from DIRAC.Core.Base.DB import DB | ||
| from DIRAC.Core.Utilities import DEncode, List, ThreadSafe, TimeUtilities | ||
|
|
@@ -14,13 +17,24 @@ | |
|
|
||
| gSynchro = ThreadSafe.Synchronizer() | ||
|
|
||
| ADDKEYVALUE_CACHE_SIZE = 2048 | ||
| GETTABLENAME_CACHE_SIZE = 128 | ||
|
|
||
|
|
||
| class AccountingDB(DB): | ||
| def __init__(self, name="Accounting/AccountingDB", readOnly=False, parentLogger=None): | ||
| def __init__(self, name="Accounting/AccountingDB", readOnly=False, parentLogger=None, accounting_types=None): | ||
| DB.__init__(self, "AccountingDB", name, parentLogger=parentLogger) | ||
|
|
||
| # Cached method | ||
| self._addkeyvalue_cache = LRUCache(maxsize=ADDKEYVALUE_CACHE_SIZE) | ||
| self._addkeyvalue_lock = threading.Lock() | ||
| self._gettablename_cache = LRUCache(maxsize=GETTABLENAME_CACHE_SIZE) | ||
| self._gettablename_lock = threading.Lock() | ||
|
|
||
| self.maxBucketTime = 604800 # 1 w | ||
| self.autoCompact = False | ||
| self.__readOnly = readOnly | ||
| self.__accounting_types = accounting_types if accounting_types else [] | ||
| self.__doingCompaction = False | ||
| self.__doingPendingLockTime = 0 | ||
| self.__deadLockRetries = 2 | ||
|
|
@@ -129,6 +143,9 @@ def __loadCatalogFromDB(self): | |
| raise Exception(retVal["Message"]) | ||
| for typesEntry in retVal["Value"]: | ||
| typeName = typesEntry[0] | ||
| if self.__accounting_types and typeName not in self.__accounting_types: | ||
| self.log.info("Ignoring accounting type as not in the list", typeName) | ||
| continue | ||
| keyFields = List.fromChar(typesEntry[1], ",") | ||
| valueFields = List.fromChar(typesEntry[2], ",") | ||
| bucketsLength = DEncode.decode(typesEntry[3].encode())[0] | ||
|
|
@@ -159,6 +176,8 @@ def loadPendingRecords(self): | |
| """ | ||
| Load all records pending to insertion and generate threaded jobs | ||
| """ | ||
| self.log.info("addkeyvalue cache", f"{self.__addKeyValue.cache.currsize}/{self.__addKeyValue.cache.maxsize}") | ||
| self.log.info("gettablename cache", f"{self._getTableName.cache.currsize}/{self._getTableName.cache.maxsize}") | ||
| gSynchro.lock() | ||
| try: | ||
| now = time.time() | ||
|
|
@@ -186,7 +205,7 @@ def loadPendingRecords(self): | |
| % self.getWaitingRecordsLifeTime() | ||
| ) | ||
| req = "SELECT " | ||
| req += ",".join(sqlFields) | ||
| req += ", ".join([f"`{f}`" for f in sqlFields]) | ||
| req += f" FROM {sqlTableName} " | ||
| req += "WHERE taken = 0 or TIMESTAMPDIFF( SECOND, takenSince, UTC_TIMESTAMP() ) > %s " | ||
| args = [self.getWaitingRecordsLifeTime()] | ||
|
|
@@ -198,6 +217,7 @@ def loadPendingRecords(self): | |
| "[PENDING] Error when trying to get pending records", | ||
| f"for {typeName} : {result['Message']}", | ||
| ) | ||
| self.__doingPendingLockTime = 0 | ||
| return result | ||
| self.log.info(f"[PENDING] Got {len(result['Value'])} pending records for type {typeName}") | ||
| dbData = result["Value"] | ||
|
|
@@ -279,6 +299,9 @@ def registerType(self, name, definitionKeyFields, definitionAccountingFields, bu | |
| """ | ||
| Register a new type | ||
| """ | ||
| if self.__accounting_types and name not in self.__accounting_types: | ||
| self.log.info("Not registering accounting type as not in the list", name) | ||
| return S_OK(False) | ||
|
|
||
| result = self.__loadTablesCreated() | ||
| if not result["OK"]: | ||
|
|
@@ -440,6 +463,7 @@ def __getIdForKeyValue(self, typeName, keyName, keyValue, conn=False): | |
| return S_OK(retVal["Value"][0][0]) | ||
| return S_ERROR(f"Key id {keyName} for value {keyValue} does not exist although it should") | ||
|
|
||
| @cachedmethod(lambda self: self._addkeyvalue_cache, lock=lambda self: self._addkeyvalue_lock) | ||
| def __addKeyValue(self, typeName, keyName, keyValue): | ||
| """ | ||
| Adds a key value to a key table if not existant | ||
|
|
@@ -566,10 +590,12 @@ def __insertFromINTable(self, recordTuples): | |
| """ | ||
| Do the real insert and delete from the in buffer table | ||
| """ | ||
| if self.__readOnly: | ||
| return S_ERROR("ReadOnly mode enabled. No modification allowed") | ||
| self.log.verbose("Received bundle to process", f"of {len(recordTuples)} elements") | ||
| for record in recordTuples: | ||
| iD, typeName, startTime, endTime, valuesList, insertionEpoch = record | ||
| result = self.insertRecordDirectly(typeName, startTime, endTime, valuesList) | ||
| result = self._insertRecordDirectly(typeName, startTime, endTime, valuesList) | ||
| if not result["OK"]: | ||
| req = "UPDATE " | ||
| req += self._getTableName("in", typeName) | ||
|
|
@@ -584,12 +610,10 @@ def __insertFromINTable(self, recordTuples): | |
| if not result["OK"]: | ||
| self.log.error("Can't delete row from the IN table", result["Message"]) | ||
|
|
||
| def insertRecordDirectly(self, typeName, startTime, endTime, valuesList): | ||
| def _insertRecordDirectly(self, typeName, startTime, endTime, valuesList): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because it is internal only |
||
| """ | ||
| Add an entry to the type contents | ||
| """ | ||
| if self.__readOnly: | ||
| return S_ERROR("ReadOnly mode enabled. No modification allowed") | ||
| self.log.info( | ||
| "Adding record", | ||
| "for type %s\n [%s -> %s]" | ||
|
|
@@ -689,7 +713,7 @@ def __writeBuckets(self, typeName, buckets, keyValues, valuesList, connObj=False | |
| sqlFields.extend(self.dbCatalog[typeName]["keys"]) | ||
| sqlFields.extend(self.dbCatalog[typeName]["values"]) | ||
| sqlUpData = ["entriesInBucket=entriesInBucket+VALUES(entriesInBucket)"] | ||
| sqlUpData.extend([f"{x}={x}+VALUES({x})" for x in self.dbCatalog[typeName]["values"]]) | ||
| sqlUpData.extend([f"`{x}`=`{x}`+VALUES(`{x}`)" for x in self.dbCatalog[typeName]["values"]]) | ||
| valueGroups = [] | ||
| sqlValues = [] | ||
| for bucketInfo in buckets: | ||
|
|
@@ -710,7 +734,7 @@ def __writeBuckets(self, typeName, buckets, keyValues, valuesList, connObj=False | |
| req = "INSERT INTO " | ||
| req += self._getTableName("bucket", typeName) | ||
| req += " (" | ||
| req += ",".join(sqlFields) | ||
| req += ", ".join([f"`{f}`" for f in sqlFields]) | ||
| req += ") VALUES " | ||
| req += ",".join(valueGroups) | ||
| req += " ON DUPLICATE KEY UPDATE " | ||
|
|
@@ -1292,6 +1316,7 @@ def __commitTransaction(self, connObj): | |
| def __rollbackTransaction(self, connObj): | ||
| return self._query("ROLLBACK", conn=connObj) | ||
|
|
||
| @cachedmethod(lambda self: self._gettablename_cache, lock=lambda self: self._gettablename_lock) | ||
| def _getTableName(self, tableType, typeName, keyName=None): | ||
| """ | ||
| Generate table name | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there somewhere a list of all the possible types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, also because this can depend on what you have in your extension.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's the main problem, and that's why it is not recommended