summaryrefslogtreecommitdiffstats
path: root/src/test/erasure-code/TestErasureCodeShec_thread.cc
blob: c3e6d8eb308f6885d953c5859010e3c77d8c15f0 (plain)
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2014,2015 FUJITSU LIMITED
 *
 * Author: Shotaro Kawaguchi <kawaguchi.s@jp.fujitsu.com>
 * Author: Takanori Nakao <nakao.takanori@jp.fujitsu.com>
 * Author: Takeshi Miyamae <miyamae.takeshi@jp.fujitsu.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 */

// SUMMARY: TestErasureCodeShec executes some threads at the same time

#include <errno.h>
#include <pthread.h>
#include <stdlib.h>

#include "crush/CrushWrapper.h"
#include "osd/osd_types.h"
#include "include/stringify.h"
#include "erasure-code/shec/ErasureCodeShec.h"
#include "erasure-code/ErasureCodePlugin.h"
#include "global/global_context.h"
#include "gtest/gtest.h"

void* thread1(void* pParam);

class TestParam {
public:
  string k, m, c, w;
};

TEST(ErasureCodeShec, thread)
{
  TestParam param1, param2, param3, param4, param5;
  param1.k = "6";
  param1.m = "4";
  param1.c = "3";
  param1.w = "8";

  param2.k = "4";
  param2.m = "3";
  param2.c = "2";
  param2.w = "16";

  param3.k = "10";
  param3.m = "8";
  param3.c = "4";
  param3.w = "32";

  param4.k = "5";
  param4.m = "5";
  param4.c = "5";
  param4.w = "8";

  param5.k = "9";
  param5.m = "9";
  param5.c = "6";
  param5.w = "16";

  pthread_t tid1, tid2, tid3, tid4, tid5;
  pthread_create(&tid1, NULL, thread1, (void*) &param1);
  std::cout << "thread1 start " << std::endl;
  pthread_create(&tid2, NULL, thread1, (void*) &param2);
  std::cout << "thread2 start " << std::endl;
  pthread_create(&tid3, NULL, thread1, (void*) &param3);
  std::cout << "thread3 start " << std::endl;
  pthread_create(&tid4, NULL, thread1, (void*) &param4);
  std::cout << "thread4 start " << std::endl;
  pthread_create(&tid5, NULL, thread1, (void*) &param5);
  std::cout << "thread5 start " << std::endl;

  pthread_join(tid1, NULL);
  pthread_join(tid2, NULL);
  pthread_join(tid3, NULL);
  pthread_join(tid4, NULL);
  pthread_join(tid5, NULL);
}

void* thread1(void* pParam)
{
  TestParam* param = static_cast<TestParam*>(pParam);

  time_t start, end;

  ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();

  instance.disable_dlclose = true;
  {
    std::lock_guard l{instance.lock};
    __erasure_code_init((char*) "shec", (char*) "");
  }
  std::cout << "__erasure_code_init finish " << std::endl;

  //encode
  bufferlist in;
  set<int> want_to_encode;
  map<int, bufferlist> encoded;

  in.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" //length = 62
	    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"//124
	    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"//186
	    "012345"//192
  );

  //decode
  int want_to_decode[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  map<int, bufferlist> decoded;
  bufferlist out1, out2, usable;

  time(&start);
  time(&end);
  const int kTestSec = 60;
  ErasureCodeShecTableCache tcache;

  while (kTestSec >= (end - start)) {
    //init
    int r;
    ErasureCodeShec* shec = new ErasureCodeShecReedSolomonVandermonde(
				    tcache,
				    ErasureCodeShec::MULTIPLE);
    ErasureCodeProfile *profile = new ErasureCodeProfile();
    (*profile)["plugin"] = "shec";
    (*profile)["technique"] = "multiple";
    (*profile)["crush-failure-domain"] = "osd";
    (*profile)["k"] = param->k;
    (*profile)["m"] = param->m;
    (*profile)["c"] = param->c;
    (*profile)["w"] = param->w;
    r = shec->init(*profile, &cerr);

    int i_k = std::atoi(param->k.c_str());
    int i_m = std::atoi(param->m.c_str());
    int i_c = std::atoi(param->c.c_str());
    int i_w = std::atoi(param->w.c_str());

    EXPECT_EQ(0, r);
    EXPECT_EQ(i_k, shec->k);
    EXPECT_EQ(i_m, shec->m);
    EXPECT_EQ(i_c, shec->c);
    EXPECT_EQ(i_w, shec->w);
    EXPECT_EQ(ErasureCodeShec::MULTIPLE, shec->technique);
    EXPECT_STREQ("default", shec->rule_root.c_str());
    EXPECT_STREQ("osd", shec->rule_failure_domain.c_str());
    EXPECT_TRUE(shec->matrix != NULL);
    if ((shec->matrix == NULL)) {
      std::cout << "matrix is null" << std::endl;
      // error
      break;
    }

    //encode
    for (unsigned int i = 0; i < shec->get_chunk_count(); i++) {
      want_to_encode.insert(i);
    }
    r = shec->encode(want_to_encode, in, &encoded);

    EXPECT_EQ(0, r);
    EXPECT_EQ(shec->get_chunk_count(), encoded.size());
    EXPECT_EQ(shec->get_chunk_size(in.length()), encoded[0].length());

    if (r != 0) {
      std::cout << "error in encode" << std::endl;
      //error
      break;
    }

    //decode
    r = shec->_decode(set<int>(want_to_decode, want_to_decode + 2),
		      encoded,
		      &decoded);

    EXPECT_EQ(0, r);
    EXPECT_EQ(2u, decoded.size());
    EXPECT_EQ(shec->get_chunk_size(in.length()), decoded[0].length());

    if (r != 0) {
      std::cout << "error in decode" << std::endl;
      //error
      break;
    }

    //out1 is "encoded"
    for (unsigned int i = 0; i < encoded.size(); i++) {
      out1.append(encoded[i]);
    }
    //out2 is "decoded"
    shec->decode_concat(encoded, &out2);
    usable.substr_of(out2, 0, in.length());
    EXPECT_FALSE(out1 == in);
    EXPECT_TRUE(usable == in);
    if (out1 == in || !(usable == in)) {
      std::cout << "encode(decode) result is not correct" << std::endl;
      break;
    }

    delete shec;
    delete profile;
    want_to_encode.clear();
    encoded.clear();
    decoded.clear();
    out1.clear();
    out2.clear();
    usable.clear();

    time(&end);
  }

  return NULL;
}