Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# Copyright (C) 2018 Bloomberg LP 

# 

# 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. 

 

 

""" 

CAS services 

================== 

 

Implements the Content Addressable Storage API and ByteStream API. 

""" 

 

 

import logging 

 

import grpc 

 

from buildgrid._exceptions import InvalidArgumentError, NotFoundError, OutOfRangeError 

from buildgrid._protos.google.bytestream import bytestream_pb2, bytestream_pb2_grpc 

from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2 

from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2_grpc 

from buildgrid.server._authentication import AuthContext, authorize 

 

 

class ContentAddressableStorageService(remote_execution_pb2_grpc.ContentAddressableStorageServicer): 

 

def __init__(self, server): 

self.__logger = logging.getLogger(__name__) 

 

self._instances = {} 

 

remote_execution_pb2_grpc.add_ContentAddressableStorageServicer_to_server(self, server) 

 

# --- Public API --- 

 

def add_instance(self, name, instance): 

self._instances[name] = instance 

 

# --- Public API: Servicer --- 

 

@authorize(AuthContext) 

def FindMissingBlobs(self, request, context): 

self.__logger.debug("FindMissingBlobs request from [%s]", context.peer()) 

 

try: 

instance = self._get_instance(request.instance_name) 

response = instance.find_missing_blobs(request.blob_digests) 

 

return response 

 

except InvalidArgumentError as e: 

self.__logger.error(e) 

context.set_details(str(e)) 

context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

 

return remote_execution_pb2.FindMissingBlobsResponse() 

 

@authorize(AuthContext) 

def BatchUpdateBlobs(self, request, context): 

self.__logger.debug("BatchUpdateBlobs request from [%s]", context.peer()) 

 

try: 

instance = self._get_instance(request.instance_name) 

response = instance.batch_update_blobs(request.requests) 

 

return response 

 

except InvalidArgumentError as e: 

self.__logger.error(e) 

context.set_details(str(e)) 

context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

 

return remote_execution_pb2.BatchReadBlobsResponse() 

 

@authorize(AuthContext) 

def BatchReadBlobs(self, request, context): 

self.__logger.debug("BatchReadBlobs request from [%s]", context.peer()) 

 

try: 

instance = self._get_instance(request.instance_name) 

response = instance.batch_read_blobs(request.digests) 

return response 

 

except InvalidArgumentError as e: 

self.__logger.error(e) 

context.set_details(str(e)) 

context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

 

return remote_execution_pb2.BatchReadBlobsResponse() 

 

@authorize(AuthContext) 

def GetTree(self, request, context): 

self.__logger.debug("GetTree request from [%s]", context.peer()) 

 

try: 

instance = self._get_instance(request.instance_name) 

yield from instance.get_tree(request) 

 

except InvalidArgumentError as e: 

self.__logger.error(e) 

context.set_details(str(e)) 

context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

 

yield remote_execution_pb2.GetTreeResponse() 

 

# --- Private API --- 

 

def _get_instance(self, instance_name): 

try: 

return self._instances[instance_name] 

 

except KeyError: 

raise InvalidArgumentError("Invalid instance name: [{}]".format(instance_name)) 

 

 

class ByteStreamService(bytestream_pb2_grpc.ByteStreamServicer): 

 

def __init__(self, server): 

self.__logger = logging.getLogger(__name__) 

 

self._instances = {} 

 

bytestream_pb2_grpc.add_ByteStreamServicer_to_server(self, server) 

 

# --- Public API --- 

 

def add_instance(self, name, instance): 

self._instances[name] = instance 

 

# --- Public API: Servicer --- 

 

@authorize(AuthContext) 

def Read(self, request, context): 

self.__logger.debug("Read request from [%s]", context.peer()) 

 

names = request.resource_name.split('/') 

 

try: 

instance_name = '' 

# Format: "{instance_name}/blobs/{hash}/{size}": 

if len(names) < 3 or names[-3] != 'blobs': 

raise InvalidArgumentError("Invalid resource name: [{}]" 

.format(request.resource_name)) 

 

elif names[0] != 'blobs': 

index = names.index('blobs') 

instance_name = '/'.join(names[:index]) 

names = names[index:] 

 

if len(names) < 3: 

raise InvalidArgumentError("Invalid resource name: [{}]" 

.format(request.resource_name)) 

 

hash_, size_bytes = names[1], names[2] 

 

instance = self._get_instance(instance_name) 

 

yield from instance.read(hash_, size_bytes, 

request.read_offset, request.read_limit) 

 

except InvalidArgumentError as e: 

self.__logger.error(e) 

context.set_details(str(e)) 

context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

yield bytestream_pb2.ReadResponse() 

 

except NotFoundError as e: 

self.__logger.error(e) 

context.set_details(str(e)) 

context.set_code(grpc.StatusCode.NOT_FOUND) 

yield bytestream_pb2.ReadResponse() 

 

except OutOfRangeError as e: 

self.__logger.error(e) 

context.set_details(str(e)) 

context.set_code(grpc.StatusCode.OUT_OF_RANGE) 

yield bytestream_pb2.ReadResponse() 

 

@authorize(AuthContext) 

def Write(self, requests, context): 

self.__logger.debug("Write request from [%s]", context.peer()) 

 

request = next(requests) 

names = request.resource_name.split('/') 

 

try: 

instance_name = '' 

# Format: "{instance_name}/uploads/{uuid}/blobs/{hash}/{size}/{anything}": 

if len(names) < 5 or 'uploads' not in names or 'blobs' not in names: 

raise InvalidArgumentError("Invalid resource name: [{}]" 

.format(request.resource_name)) 

 

elif names[0] != 'uploads': 

index = names.index('uploads') 

instance_name = '/'.join(names[:index]) 

names = names[index:] 

 

if len(names) < 5: 

raise InvalidArgumentError("Invalid resource name: [{}]" 

.format(request.resource_name)) 

 

_, hash_, size_bytes = names[1], names[3], names[4] 

 

instance = self._get_instance(instance_name) 

 

return instance.write(hash_, size_bytes, request.data, 

[request.data for request in requests]) 

 

except NotImplementedError as e: 

self.__logger.error(e) 

context.set_details(str(e)) 

context.set_code(grpc.StatusCode.UNIMPLEMENTED) 

 

except InvalidArgumentError as e: 

self.__logger.error(e) 

context.set_details(str(e)) 

context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

 

except NotFoundError as e: 

self.__logger.error(e) 

context.set_details(str(e)) 

context.set_code(grpc.StatusCode.NOT_FOUND) 

 

return bytestream_pb2.WriteResponse() 

 

@authorize(AuthContext) 

def QueryWriteStatus(self, request, context): 

context.set_code(grpc.StatusCode.UNIMPLEMENTED) 

context.set_details('Method not implemented!') 

 

return bytestream_pb2.QueryWriteStatusResponse() 

 

# --- Private API --- 

 

def _get_instance(self, instance_name): 

try: 

return self._instances[instance_name] 

 

except KeyError: 

raise InvalidArgumentError("Invalid instance name: [{}]".format(instance_name))