-
Notifications
You must be signed in to change notification settings - Fork 46
/
science_granule_ingestion_worker.py
195 lines (173 loc) · 7.86 KB
/
science_granule_ingestion_worker.py
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
#!/usr/bin/env python
'''
@author Luke Campbell <LCampbell@ASAScience.com>
@file ion/processes/data/ingestion/science_granule_ingestion_worker.py
@date 06/26/12 11:38
@description DESCRIPTION
'''
from ion.services.dm.utility.granule.record_dictionary import RecordDictionaryTool
from pyon.datastore.datastore import DataStore
from pyon.util.arg_check import validate_is_instance
from interface.services.coi.iresource_registry_service import ResourceRegistryServiceClient
from pyon.util.containers import get_ion_ts, get_safe
from pyon.public import log, RT, PRED
from ion.services.dm.inventory.dataset_management_service import DatasetManagementService
from ion.services.dm.utility.granule_utils import CoverageCraft
from interface.objects import Granule
from couchdb import ResourceNotFound
from ion.core.process.transform import TransformStreamListener
import re
import collections
import numpy
class ScienceGranuleIngestionWorker(TransformStreamListener):
CACHE_LIMIT=100
def __init__(self, *args,**kwargs):
super(ScienceGranuleIngestionWorker, self).__init__(*args, **kwargs)
#--------------------------------------------------------------------------------
# Ingestion Cache
# - Datasets
# - Coverage instances
#--------------------------------------------------------------------------------
self._datasets = collections.OrderedDict()
self._coverages = collections.OrderedDict()
def on_start(self): #pragma no cover
super(ScienceGranuleIngestionWorker,self).on_start()
self.datastore_name = self.CFG.get_safe('process.datastore_name', 'datasets')
self.db = self.container.datastore_manager.get_datastore(self.datastore_name, DataStore.DS_PROFILE.SCIDATA)
log.debug('Created datastore %s', self.datastore_name)
def on_quit(self): #pragma no cover
self.subscriber.stop()
def _new_dataset(self, stream_id):
'''
Adds a new dataset to the internal cache of the ingestion worker
'''
rr_client = ResourceRegistryServiceClient()
datasets, _ = rr_client.find_subjects(subject_type=RT.DataSet,predicate=PRED.hasStream,object=stream_id,id_only=True)
if datasets:
return datasets[0]
return None
def get_dataset(self,stream_id):
'''
Memoization (LRU) of _new_dataset
'''
try:
result = self._datasets.pop(stream_id)
except KeyError:
result = self._new_dataset(stream_id)
if result is None:
return None
if len(self._datasets) >= self.CACHE_LIMIT:
self._datasets.popitem(0)
self._datasets[stream_id] = result
return result
def get_coverage(self, stream_id):
'''
Memoization (LRU) of _get_coverage
'''
try:
result = self._coverages.pop(stream_id)
except KeyError:
dataset_id = self.get_dataset(stream_id)
if dataset_id is None:
return None
result = DatasetManagementService._get_coverage(dataset_id)
if result is None:
return None
if len(self._coverages) >= self.CACHE_LIMIT:
self._coverages.popitem(0)
self._coverages[stream_id] = result
return result
def recv_packet(self, msg, stream_route, stream_id):
'''
Actual ingestion mechanism
'''
if msg == {}:
log.error('Received empty message from stream: %s', stream_id)
return
# Message validation
validate_is_instance(msg,Granule,'Incoming message is not compatible with this ingestion worker')
log.info('Received incoming granule from route: %s and stream_id: %s', stream_route, stream_id)
granule = msg
self.add_granule(stream_id, granule)
self.persist_meta(stream_id, granule)
def persist_meta(self, stream_id, granule):
#--------------------------------------------------------------------------------
# Metadata persistence
#--------------------------------------------------------------------------------
# Determine the `time` in the granule
dataset_id = self.get_dataset(stream_id)
rdt = RecordDictionaryTool.load_from_granule(granule)
time = get_safe(rdt,'time')
if time is not None and len(time) and isinstance(time,numpy.ndarray):
time = time[0]
else:
time = None
dataset_granule = {
'stream_id' : stream_id,
'dataset_id' : dataset_id,
'persisted_sha1' : dataset_id,
'encoding_type' : 'coverage',
'ts_create' : get_ion_ts()
}
if time is not None:
dataset_granule['ts_create'] = '%s' % time
self.persist(dataset_granule)
#--------------------------------------------------------------------------------
def add_granule(self,stream_id, granule):
'''
Appends the granule's data to the coverage and persists it.
'''
#--------------------------------------------------------------------------------
# Coverage determiniation and appending
#--------------------------------------------------------------------------------
dataset_id = self.get_dataset(stream_id)
if not dataset_id:
log.error('No dataset could be determined on this stream: %s', stream_id)
return
coverage = self.get_coverage(stream_id)
if not coverage:
log.error('Could not persist coverage from granule, coverage is None')
return
#--------------------------------------------------------------------------------
# Actual persistence
#--------------------------------------------------------------------------------
rdt = RecordDictionaryTool.load_from_granule(granule)
start_index = coverage.num_timesteps
elements = len(rdt)
if not elements:
return
coverage.insert_timesteps(elements)
for k,v in rdt.iteritems():
log.info('key: %s', k)
log.info('value: %s', v)
slice_ = slice(start_index, None)
coverage.set_parameter_values(param_name=k, tdoa=slice_, value=v)
DatasetManagementService._persist_coverage(dataset_id,coverage)
def persist(self, dataset_granule): #pragma no cover
'''
Persists the dataset metadata
'''
#--------------------------------------------------------------------------------
# Theres a potential that the datastore could have been deleted while ingestion
# is still running. Essentially this refreshes the state
#--------------------------------------------------------------------------------
try:
self.db.create_doc(dataset_granule)
return
except ResourceNotFound as e:
log.error('The datastore was removed while ingesting.')
self.db = self.container.datastore_manager.get_datastore(self.datastore_name, DataStore.DS_PROFILE.SCIDATA)
log.error('Trying to ingest once more')
#--------------------------------------------------------------------------------
# The first call to create_doc attached an _id to the dictionary which causes an
# error to be raised, to make this more resilient, we investigate to ensure
# the dictionary does not have any of these excess keys
#--------------------------------------------------------------------------------
try:
if '_id' in dataset_granule:
del dataset_granule['_id']
if '_rev' in dataset_granule:
del dataset_granule['_rev']
self.db.create_doc(dataset_granule)
except ResourceNotFound as e:
log.error(e.message) # Oh well I tried