walSeek.c 3.9 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
#include "os.h"
#include "taoserror.h"
#include "tfile.h"
L
Liu Jicong 已提交
20
#include "tref.h"
L
Liu Jicong 已提交
21 22
#include "walInt.h"

L
Liu Jicong 已提交
23
static int walSeekWritePos(SWal* pWal, int64_t ver) {
L
Liu Jicong 已提交
24
  int code = 0;
L
Liu Jicong 已提交
25

L
Liu Jicong 已提交
26 27
  int64_t idxTfd = pWal->writeIdxTfd;
  int64_t logTfd = pWal->writeLogTfd;
L
Liu Jicong 已提交
28 29

  // seek position
L
Liu Jicong 已提交
30 31
  int64_t idxOff = walGetVerIdxOffset(pWal, ver);
  code = tfLseek(idxTfd, idxOff, SEEK_SET);
L
Liu Jicong 已提交
32
  if (code != 0) {
L
Liu Jicong 已提交
33
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
34
    return -1;
L
Liu Jicong 已提交
35
  }
L
Liu Jicong 已提交
36
  SWalIdxEntry entry;
L
Liu Jicong 已提交
37
  // TODO:deserialize
L
Liu Jicong 已提交
38
  code = tfRead(idxTfd, &entry, sizeof(SWalIdxEntry));
L
Liu Jicong 已提交
39
  if (code != 0) {
L
Liu Jicong 已提交
40
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
41
    return -1;
L
Liu Jicong 已提交
42
  }
L
Liu Jicong 已提交
43
  ASSERT(entry.ver == ver);
L
Liu Jicong 已提交
44
  code = tfLseek(logTfd, entry.offset, SEEK_SET);
L
Liu Jicong 已提交
45
  if (code < 0) {
L
Liu Jicong 已提交
46
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
47
    return -1;
L
Liu Jicong 已提交
48 49 50 51
  }
  return code;
}

L
Liu Jicong 已提交
52
int walSetWrite(SWal* pWal) {
L
Liu Jicong 已提交
53 54
  int64_t       idxTfd, logTfd;
  SWalFileInfo* pRet = taosArrayGetLast(pWal->fileInfoSet);
L
Liu Jicong 已提交
55 56 57 58 59
  ASSERT(pRet != NULL);
  int64_t fileFirstVer = pRet->firstVer;

  char fnameStr[WAL_FILE_LEN];
  walBuildIdxName(pWal, fileFirstVer, fnameStr);
L
Liu Jicong 已提交
60
  idxTfd = tfOpenCreateWriteAppend(fnameStr);
L
Liu Jicong 已提交
61
  if (idxTfd < 0) {
L
Liu Jicong 已提交
62
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
63 64 65
    return -1;
  }
  walBuildLogName(pWal, fileFirstVer, fnameStr);
L
Liu Jicong 已提交
66
  logTfd = tfOpenCreateWriteAppend(fnameStr);
L
Liu Jicong 已提交
67
  if (logTfd < 0) {
L
Liu Jicong 已提交
68
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
69 70
    return -1;
  }
L
Liu Jicong 已提交
71
  // switch file
L
Liu Jicong 已提交
72 73 74 75 76
  pWal->writeIdxTfd = idxTfd;
  pWal->writeLogTfd = logTfd;
  return 0;
}

L
Liu Jicong 已提交
77
int walChangeWrite(SWal* pWal, int64_t ver) {
L
Liu Jicong 已提交
78
  int     code = 0;
L
Liu Jicong 已提交
79
  int64_t idxTfd, logTfd;
L
Liu Jicong 已提交
80
  char    fnameStr[WAL_FILE_LEN];
L
Liu Jicong 已提交
81 82 83 84 85 86
  if (pWal->writeLogTfd != -1) {
    code = tfClose(pWal->writeLogTfd);
    if (code != 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
      return -1;
    }
L
Liu Jicong 已提交
87
  }
L
Liu Jicong 已提交
88 89 90 91 92 93
  if (pWal->writeIdxTfd != -1) {
    code = tfClose(pWal->writeIdxTfd);
    if (code != 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
      return -1;
    }
L
Liu Jicong 已提交
94
  }
L
Liu Jicong 已提交
95

L
Liu Jicong 已提交
96
  SWalFileInfo tmpInfo;
L
Liu Jicong 已提交
97
  tmpInfo.firstVer = ver;
L
Liu Jicong 已提交
98
  // bsearch in fileSet
L
Liu Jicong 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  int32_t idx = taosArraySearchIdx(pWal->fileInfoSet, &tmpInfo, compareWalFileInfo, TD_LE);
  ASSERT(idx != -1);
  SWalFileInfo* pFileInfo = taosArrayGet(pWal->fileInfoSet, idx);
  /*ASSERT(pFileInfo != NULL);*/

  int64_t fileFirstVer = pFileInfo->firstVer;
  walBuildIdxName(pWal, fileFirstVer, fnameStr);
  idxTfd = tfOpenCreateWriteAppend(fnameStr);
  if (idxTfd < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    pWal->writeIdxTfd = -1;
    return -1;
  }
  walBuildLogName(pWal, fileFirstVer, fnameStr);
  logTfd = tfOpenCreateWriteAppend(fnameStr);
  if (logTfd < 0) {
    tfClose(idxTfd);
    terrno = TAOS_SYSTEM_ERROR(errno);
    pWal->writeLogTfd = -1;
    return -1;
L
Liu Jicong 已提交
119 120
  }

L
Liu Jicong 已提交
121 122
  pWal->writeLogTfd = logTfd;
  pWal->writeIdxTfd = idxTfd;
L
Liu Jicong 已提交
123
  pWal->writeCur = idx;
L
Liu Jicong 已提交
124
  return fileFirstVer;
L
Liu Jicong 已提交
125 126
}

L
Liu Jicong 已提交
127
int walSeekWriteVer(SWal* pWal, int64_t ver) {
L
Liu Jicong 已提交
128
  int code;
L
Liu Jicong 已提交
129
  if (ver == pWal->vers.lastVer) {
L
Liu Jicong 已提交
130 131
    return 0;
  }
L
Liu Jicong 已提交
132
  if (ver > pWal->vers.lastVer || ver < pWal->vers.firstVer) {
L
Liu Jicong 已提交
133
    terrno = TSDB_CODE_WAL_INVALID_VER;
L
Liu Jicong 已提交
134 135
    return -1;
  }
L
Liu Jicong 已提交
136
  if (ver < pWal->vers.snapshotVer) {
L
Liu Jicong 已提交
137
    
L
Liu Jicong 已提交
138
  }
L
Liu Jicong 已提交
139
  if (ver < walGetCurFileFirstVer(pWal) || (ver > walGetCurFileLastVer(pWal))) {
L
Liu Jicong 已提交
140
    code = walChangeWrite(pWal, ver);
L
Liu Jicong 已提交
141
    if (code != 0) {
L
Liu Jicong 已提交
142 143
      return -1;
    }
L
Liu Jicong 已提交
144
  }
L
Liu Jicong 已提交
145
  code = walSeekWritePos(pWal, ver);
L
Liu Jicong 已提交
146
  if (code != 0) {
L
Liu Jicong 已提交
147 148
    return -1;
  }
L
Liu Jicong 已提交
149

L
Liu Jicong 已提交
150 151
  return 0;
}