|
| 1 | +# |
| 2 | +# Copyright 2019 GridGain Systems, Inc. and Contributors. |
| 3 | +# |
| 4 | +# Licensed under the GridGain Community Edition License (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +from typing import Iterable, Union |
| 17 | + |
| 18 | +from pyignite.datatypes import Bool, Int, Long, UUIDObject |
| 19 | +from pyignite.datatypes.internal import StructArray |
| 20 | +from pyignite.queries import Query |
| 21 | +from pyignite.queries.op_codes import OP_CACHE_PARTITIONS |
| 22 | +from pyignite.utils import is_iterable |
| 23 | +from .result import APIResult |
| 24 | + |
| 25 | + |
| 26 | +cache_ids = StructArray([ |
| 27 | + ('cache_id', Int), |
| 28 | +]) |
| 29 | + |
| 30 | +cache_config = StructArray([ |
| 31 | + ('key_type_id', Int), |
| 32 | + ('affinity_key_field_id', Int), |
| 33 | +]) |
| 34 | + |
| 35 | +node_partitions = StructArray([ |
| 36 | + ('partition_id', Int), |
| 37 | +]) |
| 38 | + |
| 39 | +node_mapping = StructArray([ |
| 40 | + ('node_uuid', UUIDObject), |
| 41 | + ('node_partitions', node_partitions) |
| 42 | +]) |
| 43 | + |
| 44 | +cache_mapping = StructArray([ |
| 45 | + ('cache_id', Int), |
| 46 | + ('cache_config', cache_config), |
| 47 | +]) |
| 48 | + |
| 49 | +partition_mapping = StructArray([ |
| 50 | + ('is_applicable', Bool), |
| 51 | + ('cache_mapping', cache_mapping), |
| 52 | + ('node_mapping', node_mapping), |
| 53 | +]) |
| 54 | + |
| 55 | + |
| 56 | +def cache_get_node_partitions( |
| 57 | + conn: 'Connection', caches: Union[int, Iterable[int]], |
| 58 | + query_id: int = None, |
| 59 | +) -> APIResult: |
| 60 | + """ |
| 61 | + Gets partition mapping for an Ignite cache or a number of caches. See |
| 62 | + “IEP-23: Best Effort Affinity for thin clients”. |
| 63 | +
|
| 64 | + :param conn: connection to Ignite server, |
| 65 | + :param caches: cache ID(s) the mapping is provided for, |
| 66 | + :param query_id: (optional) a value generated by client and returned as-is |
| 67 | + in response.query_id. When the parameter is omitted, a random value |
| 68 | + is generated, |
| 69 | + :return: API result data object. |
| 70 | + """ |
| 71 | + query_struct = Query( |
| 72 | + OP_CACHE_PARTITIONS, |
| 73 | + [ |
| 74 | + ('cache_ids', cache_ids), |
| 75 | + ], |
| 76 | + query_id=query_id |
| 77 | + ) |
| 78 | + if not is_iterable(caches): |
| 79 | + caches = [caches] |
| 80 | + |
| 81 | + result = query_struct.perform( |
| 82 | + conn, |
| 83 | + query_params={ |
| 84 | + 'cache_ids': [{'cache_id': cache} for cache in caches], |
| 85 | + }, |
| 86 | + response_config=[ |
| 87 | + ('version_major', Long), |
| 88 | + ('version_minor', Int), |
| 89 | + ('partition_mapping', partition_mapping), |
| 90 | + ], |
| 91 | + ) |
| 92 | + if result.status == 0: |
| 93 | + # tidying up the result |
| 94 | + value = { |
| 95 | + 'version': ( |
| 96 | + result.value['version_major'], |
| 97 | + result.value['version_minor'] |
| 98 | + ), |
| 99 | + 'partition_mapping': [], |
| 100 | + } |
| 101 | + for i, partition_map in enumerate(result.value['partition_mapping']): |
| 102 | + cache_id = partition_map['cache_mapping'][0]['cache_id'] |
| 103 | + value['partition_mapping'].insert( |
| 104 | + i, |
| 105 | + { |
| 106 | + 'cache_id': cache_id, |
| 107 | + 'is_applicable': partition_map['is_applicable'], |
| 108 | + } |
| 109 | + ) |
| 110 | + if partition_map['is_applicable']: |
| 111 | + value['partition_mapping'][i]['cache_config'] = { |
| 112 | + a['key_type_id']: a['affinity_key_field_id'] |
| 113 | + for a in partition_map['cache_mapping'][0]['cache_config'] |
| 114 | + } |
| 115 | + value['partition_mapping'][i]['node_mapping'] = { |
| 116 | + p['node_uuid']: [ |
| 117 | + x['partition_id'] for x in p['node_partitions'] |
| 118 | + ] |
| 119 | + for p in partition_map['node_mapping'] |
| 120 | + } |
| 121 | + result.value = value |
| 122 | + |
| 123 | + return result |
0 commit comments