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
|
#pragma once
#include "public.h"
#include "helpers.h"
#include <library/cpp/yt/assert/assert.h>
#include <util/generic/array_ref.h>
#include <util/generic/ptr.h>
#include <util/generic/singleton.h>
#include <vector>
extern "C" {
#include <contrib/libs/isa-l/include/erasure_code.h>
}
namespace NErasure {
template <class TBlobType>
static inline unsigned char* ConstCast(typename TBlobType::const_iterator blobIter) {
return const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(blobIter));
}
template <int DataPartCount, int ParityPartCount, class TCodecTraits, class TBlobType = typename TCodecTraits::TBlobType, class TMutableBlobType = typename TCodecTraits::TMutableBlobType>
std::vector<TBlobType> ISAErasureEncode(
const std::vector<unsigned char>& encodeGFTables,
const std::vector<TBlobType>& dataBlocks)
{
YT_VERIFY(dataBlocks.size() == DataPartCount);
size_t blockLength = dataBlocks.front().Size();
for (size_t i = 1; i < dataBlocks.size(); ++i) {
YT_VERIFY(dataBlocks[i].Size() == blockLength);
}
std::vector<unsigned char*> dataPointers;
for (const auto& block : dataBlocks) {
dataPointers.emplace_back(ConstCast<TBlobType>(block.Begin()));
}
std::vector<TMutableBlobType> parities(ParityPartCount);
std::vector<unsigned char*> parityPointers(ParityPartCount);
for (size_t i = 0; i < ParityPartCount; ++i) {
parities[i] = TCodecTraits::AllocateBlob(blockLength);
parityPointers[i] = ConstCast<TBlobType>(parities[i].Begin());
memset(parityPointers[i], 0, blockLength);
}
ec_encode_data(
blockLength,
DataPartCount,
ParityPartCount,
const_cast<unsigned char*>(encodeGFTables.data()),
dataPointers.data(),
parityPointers.data());
return std::vector<TBlobType>(parities.begin(), parities.end());
}
template <int DataPartCount, int ParityPartCount, class TCodecTraits, class TBlobType = typename TCodecTraits::TBlobType, class TMutableBlobType = typename TCodecTraits::TMutableBlobType>
std::vector<TBlobType> ISAErasureDecode(
const std::vector<TBlobType>& dataBlocks,
const TPartIndexList& erasedIndices,
TConstArrayRef<TPartIndexList> groups,
const std::vector<unsigned char>& fullGeneratorMatrix)
{
YT_VERIFY(dataBlocks.size() >= DataPartCount);
YT_VERIFY(erasedIndices.size() <= ParityPartCount);
size_t blockLength = dataBlocks.front().Size();
for (size_t i = 1; i < dataBlocks.size(); ++i) {
YT_VERIFY(dataBlocks[i].Size() == blockLength);
}
std::vector<unsigned char> partialGeneratorMatrix(DataPartCount * DataPartCount, 0);
std::vector<unsigned char*> recoveryBlocks;
for (size_t i = 0; i < DataPartCount; ++i) {
recoveryBlocks.emplace_back(ConstCast<TBlobType>(dataBlocks[i].Begin()));
}
// Groups check is specific for LRC.
std::vector<int> isGroupHealthy(2, 1);
for (size_t i = 0; i < 2; ++i) {
for (const auto& index : erasedIndices) {
if (!groups.empty() && Contains(groups[0], index)) {
isGroupHealthy[0] = 0;
} else if (!groups.empty() && Contains(groups[1], index)) {
isGroupHealthy[1] = 0;
}
}
}
// When a group is healthy we cannot use its local parity, thus skip it using gap.
size_t gap = 0;
size_t decodeMatrixIndex = 0;
size_t erasedBlockIndex = 0;
while (decodeMatrixIndex < DataPartCount) {
size_t globalIndex = decodeMatrixIndex + erasedBlockIndex + gap;
if (erasedBlockIndex < erasedIndices.size() &&
globalIndex == static_cast<size_t>(erasedIndices[erasedBlockIndex]))
{
++erasedBlockIndex;
continue;
}
if (!groups.empty() && globalIndex >= DataPartCount && globalIndex < DataPartCount + 2) {
if (Contains(groups[0], globalIndex) && isGroupHealthy[0]) {
++gap;
continue;
}
if (Contains(groups[1], globalIndex) && isGroupHealthy[1]) {
++gap;
continue;
}
}
memcpy(&partialGeneratorMatrix[decodeMatrixIndex * DataPartCount], &fullGeneratorMatrix[globalIndex * DataPartCount], DataPartCount);
++decodeMatrixIndex;
}
std::vector<unsigned char> invertedGeneratorMatrix(DataPartCount * DataPartCount, 0);
int res = gf_invert_matrix(partialGeneratorMatrix.data(), invertedGeneratorMatrix.data(), DataPartCount);
YT_VERIFY(res == 0);
std::vector<unsigned char> decodeMatrix(DataPartCount * (DataPartCount + ParityPartCount), 0);
//! Some magical code from library example.
for (size_t i = 0; i < erasedIndices.size(); ++i) {
if (erasedIndices[i] < DataPartCount) {
memcpy(&decodeMatrix[i * DataPartCount], &invertedGeneratorMatrix[erasedIndices[i] * DataPartCount], DataPartCount);
} else {
for (int k = 0; k < DataPartCount; ++k) {
int val = 0;
for (int j = 0; j < DataPartCount; ++j) {
val ^= gf_mul_erasure(invertedGeneratorMatrix[j * DataPartCount + k], fullGeneratorMatrix[DataPartCount * erasedIndices[i] + j]);
}
decodeMatrix[DataPartCount * i + k] = val;
}
}
}
std::vector<unsigned char> decodeGFTables(DataPartCount * erasedIndices.size() * 32);
ec_init_tables(DataPartCount, erasedIndices.size(), decodeMatrix.data(), decodeGFTables.data());
std::vector<TMutableBlobType> recoveredParts;
std::vector<unsigned char*> recoveredPartsPointers;
for (size_t i = 0; i < erasedIndices.size(); ++i) {
recoveredParts.emplace_back(TCodecTraits::AllocateBlob(blockLength));
recoveredPartsPointers.emplace_back(ConstCast<TBlobType>(recoveredParts.back().Begin()));
memset(recoveredPartsPointers.back(), 0, blockLength);
}
ec_encode_data(
blockLength,
DataPartCount,
erasedIndices.size(),
decodeGFTables.data(),
recoveryBlocks.data(),
recoveredPartsPointers.data());
return std::vector<TBlobType>(recoveredParts.begin(), recoveredParts.end());
}
} // namespace NErasure
|