metaOpen.c 10.7 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

H
Hongze Cheng 已提交
16
#include "meta.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18 19 20 21 22
static int tbDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
static int skmDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
static int ctbIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
static int tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
static int ttlIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
H
Hongze Cheng 已提交
23
static int uidIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
24
static int smaIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2);
H
Hongze Cheng 已提交
25

H
Hongze Cheng 已提交
26 27 28
static int32_t metaInitLock(SMeta *pMeta) { return taosThreadRwlockInit(&pMeta->lock, NULL); }
static int32_t metaDestroyLock(SMeta *pMeta) { return taosThreadRwlockDestroy(&pMeta->lock); }

H
Hongze Cheng 已提交
29 30
int metaOpen(SVnode *pVnode, SMeta **ppMeta) {
  SMeta *pMeta = NULL;
H
Hongze Cheng 已提交
31
  int    ret;
H
Hongze Cheng 已提交
32 33 34 35 36 37 38 39 40 41 42
  int    slen;

  *ppMeta = NULL;

  // create handle
  slen = strlen(tfsGetPrimaryPath(pVnode->pTfs)) + strlen(pVnode->path) + strlen(VNODE_META_DIR) + 3;
  if ((pMeta = taosMemoryCalloc(1, sizeof(*pMeta) + slen)) == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

H
Hongze Cheng 已提交
43
  metaInitLock(pMeta);
H
Hongze Cheng 已提交
44 45 46
  pMeta->path = (char *)&pMeta[1];
  sprintf(pMeta->path, "%s%s%s%s%s", tfsGetPrimaryPath(pVnode->pTfs), TD_DIRSEP, pVnode->path, TD_DIRSEP,
          VNODE_META_DIR);
wafwerar's avatar
wafwerar 已提交
47
  taosRealPath(pMeta->path, NULL, slen);
H
Hongze Cheng 已提交
48 49 50 51 52
  pMeta->pVnode = pVnode;

  // create path if not created yet
  taosMkDir(pMeta->path);

H
Hongze Cheng 已提交
53
  // open env
H
Hongze Cheng 已提交
54
  ret = tdbOpen(pMeta->path, pVnode->config.szPage, pVnode->config.szCache, &pMeta->pEnv);
H
Hongze Cheng 已提交
55
  if (ret < 0) {
S
Shengliang Guan 已提交
56
    metaError("vgId:%d, failed to open meta env since %s", TD_VID(pVnode), tstrerror(terrno));
H
Hongze Cheng 已提交
57 58 59 60
    goto _err;
  }

  // open pTbDb
H
Hongze Cheng 已提交
61
  ret = tdbTbOpen("table.db", sizeof(STbDbKey), -1, tbDbKeyCmpr, pMeta->pEnv, &pMeta->pTbDb);
H
Hongze Cheng 已提交
62
  if (ret < 0) {
S
Shengliang Guan 已提交
63
    metaError("vgId:%d, failed to open meta table db since %s", TD_VID(pVnode), tstrerror(terrno));
H
Hongze Cheng 已提交
64 65 66 67
    goto _err;
  }

  // open pSkmDb
H
Hongze Cheng 已提交
68
  ret = tdbTbOpen("schema.db", sizeof(SSkmDbKey), -1, skmDbKeyCmpr, pMeta->pEnv, &pMeta->pSkmDb);
H
Hongze Cheng 已提交
69
  if (ret < 0) {
S
Shengliang Guan 已提交
70
    metaError("vgId:%d, failed to open meta schema db since %s", TD_VID(pVnode), tstrerror(terrno));
H
Hongze Cheng 已提交
71 72 73
    goto _err;
  }

H
Hongze Cheng 已提交
74
  // open pUidIdx
H
Hongze Cheng 已提交
75
  ret = tdbTbOpen("uid.idx", sizeof(tb_uid_t), sizeof(int64_t), uidIdxKeyCmpr, pMeta->pEnv, &pMeta->pUidIdx);
H
Hongze Cheng 已提交
76
  if (ret < 0) {
S
Shengliang Guan 已提交
77
    metaError("vgId:%d, failed to open meta uid idx since %s", TD_VID(pVnode), tstrerror(terrno));
H
Hongze Cheng 已提交
78 79 80
    goto _err;
  }

H
Hongze Cheng 已提交
81
  // open pNameIdx
H
Hongze Cheng 已提交
82
  ret = tdbTbOpen("name.idx", -1, sizeof(tb_uid_t), NULL, pMeta->pEnv, &pMeta->pNameIdx);
H
Hongze Cheng 已提交
83
  if (ret < 0) {
S
Shengliang Guan 已提交
84
    metaError("vgId:%d, failed to open meta name index since %s", TD_VID(pVnode), tstrerror(terrno));
H
Hongze Cheng 已提交
85 86 87
    goto _err;
  }

H
Hongze Cheng 已提交
88
  // open pCtbIdx
H
Hongze Cheng 已提交
89
  ret = tdbTbOpen("ctb.idx", sizeof(SCtbIdxKey), 0, ctbIdxKeyCmpr, pMeta->pEnv, &pMeta->pCtbIdx);
H
Hongze Cheng 已提交
90
  if (ret < 0) {
S
Shengliang Guan 已提交
91
    metaError("vgId:%d, failed to open meta child table index since %s", TD_VID(pVnode), tstrerror(terrno));
H
Hongze Cheng 已提交
92 93 94
    goto _err;
  }

C
Cary Xu 已提交
95 96 97 98 99 100 101
  // open pSuidIdx
  ret = tdbTbOpen("suid.idx", sizeof(tb_uid_t), 0, uidIdxKeyCmpr, pMeta->pEnv, &pMeta->pSuidIdx);
  if (ret < 0) {
    metaError("vgId:%d, failed to open meta super table index since %s", TD_VID(pVnode), tstrerror(terrno));
    goto _err;
  }

H
Hongze Cheng 已提交
102
  // open pTagIdx
dengyihao's avatar
dengyihao 已提交
103 104 105 106 107 108
  // TODO(yihaoDeng), refactor later
  char indexFullPath[128] = {0};
  sprintf(indexFullPath, "%s/%s", pMeta->path, "invert");
  taosMkDir(indexFullPath);
  ret = indexOpen(indexOptsCreate(), indexFullPath, (SIndex **)&pMeta->pTagIvtIdx);
  if (ret < 0) {
S
Shengliang Guan 已提交
109
    metaError("vgId:%d, failed to open meta tag index since %s", TD_VID(pVnode), tstrerror(terrno));
dengyihao's avatar
dengyihao 已提交
110 111 112
    goto _err;
  }

H
Hongze Cheng 已提交
113
  ret = tdbTbOpen("tag.idx", -1, 0, tagIdxKeyCmpr, pMeta->pEnv, &pMeta->pTagIdx);
H
Hongze Cheng 已提交
114
  if (ret < 0) {
S
Shengliang Guan 已提交
115
    metaError("vgId:%d, failed to open meta tag index since %s", TD_VID(pVnode), tstrerror(terrno));
H
Hongze Cheng 已提交
116 117 118 119
    goto _err;
  }

  // open pTtlIdx
H
Hongze Cheng 已提交
120
  ret = tdbTbOpen("ttl.idx", sizeof(STtlIdxKey), 0, ttlIdxKeyCmpr, pMeta->pEnv, &pMeta->pTtlIdx);
H
Hongze Cheng 已提交
121
  if (ret < 0) {
S
Shengliang Guan 已提交
122
    metaError("vgId:%d, failed to open meta ttl index since %s", TD_VID(pVnode), tstrerror(terrno));
H
Hongze Cheng 已提交
123 124 125
    goto _err;
  }

C
Cary Xu 已提交
126
  // open pSmaIdx
H
Hongze Cheng 已提交
127
  ret = tdbTbOpen("sma.idx", sizeof(SSmaIdxKey), 0, smaIdxKeyCmpr, pMeta->pEnv, &pMeta->pSmaIdx);
C
Cary Xu 已提交
128
  if (ret < 0) {
S
Shengliang Guan 已提交
129
    metaError("vgId:%d, failed to open meta sma index since %s", TD_VID(pVnode), tstrerror(terrno));
C
Cary Xu 已提交
130 131 132
    goto _err;
  }

H
Hongze Cheng 已提交
133
  // open index
H
Hongze Cheng 已提交
134
  if (metaOpenIdx(pMeta) < 0) {
S
Shengliang Guan 已提交
135
    metaError("vgId:%d, failed to open meta index since %s", TD_VID(pVnode), tstrerror(terrno));
H
Hongze Cheng 已提交
136 137
    goto _err;
  }
H
Hongze Cheng 已提交
138

S
Shengliang Guan 已提交
139
  metaDebug("vgId:%d, meta is opened", TD_VID(pVnode));
H
Hongze Cheng 已提交
140 141 142 143 144 145

  *ppMeta = pMeta;
  return 0;

_err:
  if (pMeta->pIdx) metaCloseIdx(pMeta);
H
Hongze Cheng 已提交
146 147
  if (pMeta->pSmaIdx) tdbTbClose(pMeta->pSmaIdx);
  if (pMeta->pTtlIdx) tdbTbClose(pMeta->pTtlIdx);
dengyihao's avatar
dengyihao 已提交
148
  if (pMeta->pTagIvtIdx) indexClose(pMeta->pTagIvtIdx);
H
Hongze Cheng 已提交
149 150
  if (pMeta->pTagIdx) tdbTbClose(pMeta->pTagIdx);
  if (pMeta->pCtbIdx) tdbTbClose(pMeta->pCtbIdx);
C
Cary Xu 已提交
151
  if (pMeta->pSuidIdx) tdbTbClose(pMeta->pSuidIdx);
H
Hongze Cheng 已提交
152 153 154 155 156
  if (pMeta->pNameIdx) tdbTbClose(pMeta->pNameIdx);
  if (pMeta->pUidIdx) tdbTbClose(pMeta->pUidIdx);
  if (pMeta->pSkmDb) tdbTbClose(pMeta->pSkmDb);
  if (pMeta->pTbDb) tdbTbClose(pMeta->pTbDb);
  if (pMeta->pEnv) tdbClose(pMeta->pEnv);
H
Hongze Cheng 已提交
157
  metaDestroyLock(pMeta);
H
Hongze Cheng 已提交
158 159 160 161 162 163
  taosMemoryFree(pMeta);
  return -1;
}

int metaClose(SMeta *pMeta) {
  if (pMeta) {
H
Hongze Cheng 已提交
164
    if (pMeta->pIdx) metaCloseIdx(pMeta);
H
Hongze Cheng 已提交
165 166
    if (pMeta->pSmaIdx) tdbTbClose(pMeta->pSmaIdx);
    if (pMeta->pTtlIdx) tdbTbClose(pMeta->pTtlIdx);
dengyihao's avatar
dengyihao 已提交
167
    if (pMeta->pTagIvtIdx) indexClose(pMeta->pTagIvtIdx);
H
Hongze Cheng 已提交
168 169
    if (pMeta->pTagIdx) tdbTbClose(pMeta->pTagIdx);
    if (pMeta->pCtbIdx) tdbTbClose(pMeta->pCtbIdx);
C
Cary Xu 已提交
170
    if (pMeta->pSuidIdx) tdbTbClose(pMeta->pSuidIdx);
H
Hongze Cheng 已提交
171 172 173 174 175
    if (pMeta->pNameIdx) tdbTbClose(pMeta->pNameIdx);
    if (pMeta->pUidIdx) tdbTbClose(pMeta->pUidIdx);
    if (pMeta->pSkmDb) tdbTbClose(pMeta->pSkmDb);
    if (pMeta->pTbDb) tdbTbClose(pMeta->pTbDb);
    if (pMeta->pEnv) tdbClose(pMeta->pEnv);
H
Hongze Cheng 已提交
176
    metaDestroyLock(pMeta);
H
Hongze Cheng 已提交
177 178 179 180
    taosMemoryFree(pMeta);
  }

  return 0;
H
Hongze Cheng 已提交
181 182
}

H
Hongze Cheng 已提交
183 184 185 186 187 188
int32_t metaRLock(SMeta *pMeta) { return taosThreadRwlockRdlock(&pMeta->lock); }

int32_t metaWLock(SMeta *pMeta) { return taosThreadRwlockWrlock(&pMeta->lock); }

int32_t metaULock(SMeta *pMeta) { return taosThreadRwlockUnlock(&pMeta->lock); }

H
Hongze Cheng 已提交
189
static int tbDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
H
Hongze Cheng 已提交
190 191
  STbDbKey *pTbDbKey1 = (STbDbKey *)pKey1;
  STbDbKey *pTbDbKey2 = (STbDbKey *)pKey2;
H
Hongze Cheng 已提交
192

H
Hongze Cheng 已提交
193
  if (pTbDbKey1->version > pTbDbKey2->version) {
H
Hongze Cheng 已提交
194
    return 1;
H
Hongze Cheng 已提交
195 196 197 198 199 200 201
  } else if (pTbDbKey1->version < pTbDbKey2->version) {
    return -1;
  }

  if (pTbDbKey1->uid > pTbDbKey2->uid) {
    return 1;
  } else if (pTbDbKey1->uid < pTbDbKey2->uid) {
H
Hongze Cheng 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    return -1;
  }

  return 0;
}

static int skmDbKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
  SSkmDbKey *pSkmDbKey1 = (SSkmDbKey *)pKey1;
  SSkmDbKey *pSkmDbKey2 = (SSkmDbKey *)pKey2;

  if (pSkmDbKey1->uid > pSkmDbKey2->uid) {
    return 1;
  } else if (pSkmDbKey1->uid < pSkmDbKey2->uid) {
    return -1;
  }

  if (pSkmDbKey1->sver > pSkmDbKey2->sver) {
    return 1;
  } else if (pSkmDbKey1->sver < pSkmDbKey2->sver) {
    return -1;
H
Hongze Cheng 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234
  }

  return 0;
}

static int uidIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
  tb_uid_t uid1 = *(tb_uid_t *)pKey1;
  tb_uid_t uid2 = *(tb_uid_t *)pKey2;

  if (uid1 > uid2) {
    return 1;
  } else if (uid1 < uid2) {
    return -1;
H
Hongze Cheng 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
  }

  return 0;
}

static int ctbIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
  SCtbIdxKey *pCtbIdxKey1 = (SCtbIdxKey *)pKey1;
  SCtbIdxKey *pCtbIdxKey2 = (SCtbIdxKey *)pKey2;

  if (pCtbIdxKey1->suid > pCtbIdxKey2->suid) {
    return 1;
  } else if (pCtbIdxKey1->suid < pCtbIdxKey2->suid) {
    return -1;
  }

  if (pCtbIdxKey1->uid > pCtbIdxKey2->uid) {
    return 1;
  } else if (pCtbIdxKey1->uid < pCtbIdxKey2->uid) {
    return -1;
  }

  return 0;
}

static int tagIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
  STagIdxKey *pTagIdxKey1 = (STagIdxKey *)pKey1;
  STagIdxKey *pTagIdxKey2 = (STagIdxKey *)pKey2;
H
Hongze Cheng 已提交
262
  tb_uid_t    uid1, uid2;
H
Hongze Cheng 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
  int         c;

  // compare suid
  if (pTagIdxKey1->suid > pTagIdxKey2->suid) {
    return 1;
  } else if (pTagIdxKey1->suid < pTagIdxKey2->suid) {
    return -1;
  }

  // compare column id
  if (pTagIdxKey1->cid > pTagIdxKey2->cid) {
    return 1;
  } else if (pTagIdxKey1->cid < pTagIdxKey2->cid) {
    return -1;
  }

H
Hongze Cheng 已提交
279
  ASSERT(pTagIdxKey1->type == pTagIdxKey2->type);
H
Hongze Cheng 已提交
280

H
Hongze Cheng 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
  // check NULL, NULL is always the smallest
  if (pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
    return -1;
  } else if (!pTagIdxKey1->isNull && pTagIdxKey2->isNull) {
    return 1;
  } else if (!pTagIdxKey1->isNull && !pTagIdxKey2->isNull) {
    // all not NULL, compr tag vals
    c = doCompare(pTagIdxKey1->data, pTagIdxKey2->data, pTagIdxKey1->type, 0);
    if (c) return c;

    if (IS_VAR_DATA_TYPE(pTagIdxKey1->type)) {
      uid1 = *(tb_uid_t *)(pTagIdxKey1->data + varDataTLen(pTagIdxKey1->data));
      uid2 = *(tb_uid_t *)(pTagIdxKey2->data + varDataTLen(pTagIdxKey2->data));
    } else {
      uid1 = *(tb_uid_t *)(pTagIdxKey1->data + tDataTypes[pTagIdxKey1->type].bytes);
      uid2 = *(tb_uid_t *)(pTagIdxKey2->data + tDataTypes[pTagIdxKey2->type].bytes);
    }
H
Hongze Cheng 已提交
298 299
  }

H
Hongze Cheng 已提交
300 301
  // compare uid
  if (uid1 < uid2) {
H
Hongze Cheng 已提交
302
    return -1;
H
Hongze Cheng 已提交
303 304 305 306
  } else if (uid1 > uid2) {
    return 1;
  } else {
    return 0;
H
Hongze Cheng 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
  }

  return 0;
}

static int ttlIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
  STtlIdxKey *pTtlIdxKey1 = (STtlIdxKey *)pKey1;
  STtlIdxKey *pTtlIdxKey2 = (STtlIdxKey *)pKey2;

  if (pTtlIdxKey1->dtime > pTtlIdxKey2->dtime) {
    return 1;
  } else if (pTtlIdxKey1->dtime < pTtlIdxKey2->dtime) {
    return -1;
  }

  if (pTtlIdxKey1->uid > pTtlIdxKey2->uid) {
    return 1;
  } else if (pTtlIdxKey1->uid < pTtlIdxKey2->uid) {
    return -1;
  }

  return 0;
}
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348

static int smaIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
  SSmaIdxKey *pSmaIdxKey1 = (SSmaIdxKey *)pKey1;
  SSmaIdxKey *pSmaIdxKey2 = (SSmaIdxKey *)pKey2;

  if (pSmaIdxKey1->uid > pSmaIdxKey2->uid) {
    return 1;
  } else if (pSmaIdxKey1->uid < pSmaIdxKey2->uid) {
    return -1;
  }

  if (pSmaIdxKey1->smaUid > pSmaIdxKey2->smaUid) {
    return 1;
  } else if (pSmaIdxKey1->smaUid < pSmaIdxKey2->smaUid) {
    return -1;
  }

  return 0;
}