|
|
1
|
+# Copyright (C) 2018 Bloomberg LP
|
|
|
2
|
+#
|
|
|
3
|
+# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
4
|
+# you may not use this file except in compliance with the License.
|
|
|
5
|
+# You may obtain a copy of the License at
|
|
|
6
|
+#
|
|
|
7
|
+# <http://www.apache.org/licenses/LICENSE-2.0>
|
|
|
8
|
+#
|
|
|
9
|
+# Unless required by applicable law or agreed to in writing, software
|
|
|
10
|
+# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
11
|
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
12
|
+# See the License for the specific language governing permissions and
|
|
|
13
|
+# limitations under the License.
|
|
|
14
|
+
|
|
|
15
|
+
|
|
|
16
|
+"""
|
|
|
17
|
+RemoteStorage
|
|
|
18
|
+==================
|
|
|
19
|
+
|
|
|
20
|
+Forwwards storage requests to a remote storage.
|
|
|
21
|
+"""
|
|
|
22
|
+
|
|
|
23
|
+import io
|
|
|
24
|
+import logging
|
|
|
25
|
+
|
|
|
26
|
+import grpc
|
|
|
27
|
+
|
|
|
28
|
+from buildgrid.utils import create_digest, gen_fetch_blob, gen_write_request_blob
|
|
|
29
|
+from buildgrid._protos.google.bytestream import bytestream_pb2_grpc
|
|
|
30
|
+from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2, remote_execution_pb2_grpc
|
|
|
31
|
+
|
|
|
32
|
+from .storage_abc import StorageABC
|
|
|
33
|
+
|
|
|
34
|
+
|
|
|
35
|
+class RemoteStorage(StorageABC):
|
|
|
36
|
+
|
|
|
37
|
+ def __init__(self, channel, instance_name):
|
|
|
38
|
+ self.logger = logging.getLogger(__name__)
|
|
|
39
|
+ self._instance_name = instance_name
|
|
|
40
|
+ self._stub_bs = bytestream_pb2_grpc.ByteStreamStub(channel)
|
|
|
41
|
+ self._stub_cas = remote_execution_pb2_grpc.ContentAddressableStorageStub(channel)
|
|
|
42
|
+
|
|
|
43
|
+ def has_blob(self, digest):
|
|
|
44
|
+ try:
|
|
|
45
|
+ self.get_blob(digest)
|
|
|
46
|
+ return True
|
|
|
47
|
+
|
|
|
48
|
+ except grpc.RpcError:
|
|
|
49
|
+ pass
|
|
|
50
|
+
|
|
|
51
|
+ return False
|
|
|
52
|
+
|
|
|
53
|
+ def get_blob(self, digest):
|
|
|
54
|
+ fetched_data = None
|
|
|
55
|
+ for data in gen_fetch_blob(self._stub_bs, digest, self._instance_name):
|
|
|
56
|
+ fetched_data += data
|
|
|
57
|
+
|
|
|
58
|
+ assert digest.size_bytes == len(fetched_data)
|
|
|
59
|
+
|
|
|
60
|
+ return io.BytesIO(fetched_data)
|
|
|
61
|
+
|
|
|
62
|
+ def begin_write(self, digest):
|
|
|
63
|
+ return gen_write_request_blob(digest, self._instance_name)
|
|
|
64
|
+
|
|
|
65
|
+ def commit_write(self, digest, write_session):
|
|
|
66
|
+ for request in write_session:
|
|
|
67
|
+ self._stub_bs.Write(request)
|
|
|
68
|
+
|
|
|
69
|
+ def missing_blobs(self, blobs):
|
|
|
70
|
+ digests = []
|
|
|
71
|
+ for blob in blobs:
|
|
|
72
|
+ digests.append(create_digest(blob))
|
|
|
73
|
+
|
|
|
74
|
+ request = remote_execution_pb2.FindMissingBlobsRequest(instance_name=self._instance_name,
|
|
|
75
|
+ blob_digests=digests)
|
|
|
76
|
+ response = self._stub_cas.FindMissingBlobs(request)
|
|
|
77
|
+
|
|
|
78
|
+ return response.missing_blob_digests
|
|
|
79
|
+
|
|
|
80
|
+ def bulk_update_blobs(self, blobs):
|
|
|
81
|
+ requests = []
|
|
|
82
|
+ for blob in blobs:
|
|
|
83
|
+ request = remote_execution_pb2.BatchUpdateBlobsRequest.Request(digest=create_digest(blob),
|
|
|
84
|
+ data=blob)
|
|
|
85
|
+ requests.append(request)
|
|
|
86
|
+
|
|
|
87
|
+ response = self._stub_cas.BatchUpdateBlobs(instance_name=self._instance_name,
|
|
|
88
|
+ requests=requests)
|
|
|
89
|
+ result = [x.status for x in response.responses]
|
|
|
90
|
+ return result
|