-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcheckpointer.py
More file actions
266 lines (217 loc) · 8.56 KB
/
checkpointer.py
File metadata and controls
266 lines (217 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#
# Copyright 2025 Splunk Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""This module provides two kinds of checkpointer: KVStoreCheckpointer,
FileCheckpointer for modular input to save checkpoint."""
import base64
import json
import os
import os.path as op
import traceback
import warnings
from abc import ABCMeta, abstractmethod
from typing import Any, Dict, Iterable, Optional
from splunklib import binding
from solnlib import _utils, utils
from solnlib.utils import get_solnlib_logger
logger = get_solnlib_logger(__name__)
__all__ = ["CheckpointerException", "KVStoreCheckpointer", "FileCheckpointer"]
class CheckpointerException(Exception):
pass
class Checkpointer(metaclass=ABCMeta):
"""Base class of checkpointer."""
@abstractmethod
def update(self, key: str, state: Any):
"""Updates document with an id that equals to `key` and `state` as
document data."""
@abstractmethod
def batch_update(self, states: Iterable[Dict[str, Any]]):
"""Updates multiple documents."""
@abstractmethod
def get(self, key: str) -> dict:
"""Gets document with an id that equals to `key`."""
@abstractmethod
def delete(self, key: str):
"""Deletes document with an id that equals to `key`."""
class KVStoreCheckpointer(Checkpointer):
"""KVStore checkpointer.
Use KVStore to save modular input checkpoint.
More information about KV Store in Splunk is
[here](https://dev.splunk.com/enterprise/docs/developapps/manageknowledge/kvstore/aboutkvstorecollections).
Examples:
>>> from solnlib.modular_input import checkpointer
>>> checkpoint = checkpointer.KVStoreCheckpointer(
"unique_addon_checkpoints",
"session_key",
"unique_addon"
)
>>> checkpoint.update("input_1", {"timestamp": 1638043093})
>>> checkpoint.get("input_1")
>>> # returns {"timestamp": 1638043093}
"""
def __init__(
self,
collection_name: str,
session_key: str,
app: str,
owner: Optional[str] = "nobody",
scheme: Optional[str] = None,
host: Optional[str] = None,
port: Optional[int] = None,
**context: Any,
):
"""Initializes KVStoreCheckpointer.
Arguments:
collection_name: Collection name of kvstore checkpointer.
session_key: Splunk access token.
app: App name of namespace.
owner: (optional) Owner of namespace, default is `nobody`.
scheme: (optional) The access scheme, default is None.
host: (optional) The host name, default is None.
port: (optional) The port number, default is None.
context: Other configurations for Splunk rest client.
Raises:
binding.HTTPError: HTTP error different from 404, for example 503
when KV Store is initializing and not ready to serve requests.
CheckpointerException: If init KV Store checkpointer failed.
"""
try:
if not context.get("pool_connections"):
context["pool_connections"] = 5
if not context.get("pool_maxsize"):
context["pool_maxsize"] = 5
self._collection_data = _utils.get_collection_data(
collection_name,
session_key,
app,
owner,
scheme,
host,
port,
{"state": "string"},
**context,
)
except KeyError:
raise CheckpointerException("Get KV Store checkpointer failed.")
@utils.retry(exceptions=[binding.HTTPError])
def update(self, key: str, state: Any) -> None:
"""Updates document with an id that equals to `key` and `state` as
document data.
Arguments:
key: `id` of the document to update.
state: Document data to update. It can be integer, string,
or a dict, or anything that can be an argument to `json.dumps`.
Raises:
binding.HTTPError: when an error occurred in Splunk, for example,
when Splunk is restarting and KV Store is not yet initialized.
"""
record = {"_key": key, "state": json.dumps(state)}
self._collection_data.batch_save(record)
@utils.retry(exceptions=[binding.HTTPError])
def batch_update(self, states: Iterable[Dict[str, Any]]) -> None:
"""Updates multiple documents.
Arguments:
states: Iterable that contains documents to update. Document should
be a dict with at least "state" key.
Raises:
binding.HTTPError: when an error occurred in Splunk, for example,
when Splunk is restarting and KV Store is not yet initialized.
"""
for state in states:
state["state"] = json.dumps(state["state"])
self._collection_data.batch_save(*states)
@utils.retry(exceptions=[binding.HTTPError])
def get(self, key: str) -> Optional[Any]:
"""Gets document with an id that equals to `key`.
Arguments:
key: `id` of the document to get.
Raises:
binding.HTTPError: When an error occurred in Splunk (not 404 code),
can be 503 code, when Splunk is restarting and KV Store is not
yet initialized.
Returns:
Document data under `key` or `None` in case of no data.
"""
try:
record = self._collection_data.query_by_id(key)
except binding.HTTPError as e:
if e.status != 404:
logger().error(f"Get checkpoint failed: {traceback.format_exc()}.")
raise
return None
return json.loads(record["state"])
@utils.retry(exceptions=[binding.HTTPError])
def delete(self, key: str) -> None:
"""Deletes document with an id that equals to `key`.
Arguments:
key: `id` of the document to delete.
Raises:
binding.HTTPError: When an error occurred in Splunk (not 404 code),
can be 503 code, when Splunk is restarting and KV Store is not
yet initialized.
"""
try:
self._collection_data.delete_by_id(key)
except binding.HTTPError as e:
if e.status != 404:
logger().error(f"Delete checkpoint failed: {traceback.format_exc()}.")
raise
class FileCheckpointer(Checkpointer):
"""File checkpointer.
Use file to save modular input checkpoint.
Examples:
>>> from solnlib.modular_input import checkpointer
>>> ck = checkpointer.FileCheckpointer('/opt/splunk/var/...')
>>> ck.update(...)
>>> ck.get(...)
"""
def __init__(self, checkpoint_dir: str):
"""Initializes FileCheckpointer.
Arguments:
checkpoint_dir: Checkpoint directory.
"""
warnings.warn(
"FileCheckpointer is deprecated, please use KVStoreCheckpointer",
stacklevel=2,
)
self._checkpoint_dir = checkpoint_dir
def encode_key(self, key):
return base64.b64encode(key.encode()).decode()
def update(self, key, state):
file_name = op.join(self._checkpoint_dir, self.encode_key(key))
with open(file_name + "_new", "w") as fp:
json.dump(state, fp)
if op.exists(file_name):
try:
os.remove(file_name)
except OSError:
pass
os.rename(file_name + "_new", file_name)
def batch_update(self, states):
for state in states:
self.update(state["_key"], state["state"])
def get(self, key):
file_name = op.join(self._checkpoint_dir, self.encode_key(key))
try:
with open(file_name) as fp:
return json.load(fp)
except (OSError, ValueError):
return None
def delete(self, key):
file_name = op.join(self._checkpoint_dir, self.encode_key(key))
try:
os.remove(file_name)
except OSError:
pass