walSeek.c 3.6 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 walSeekFilePos(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 44 45
  ASSERT(entry.ver == ver);
  code = tfLseek(logTfd, entry.offset, SEEK_CUR);
  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 walChangeFileToLast(SWal* pWal) {
L
Liu Jicong 已提交
53 54
  int64_t       idxTfd, logTfd;
  SWalFileInfo* pRet = taosArrayGetLast(pWal->fileInfoSet);
L
Liu Jicong 已提交
55 56 57 58 59 60
  ASSERT(pRet != NULL);
  int64_t fileFirstVer = pRet->firstVer;

  char fnameStr[WAL_FILE_LEN];
  walBuildIdxName(pWal, fileFirstVer, fnameStr);
  idxTfd = tfOpenReadWrite(fnameStr);
L
Liu Jicong 已提交
61
  if (idxTfd < 0) {
L
Liu Jicong 已提交
62
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
63 64 65 66
    return -1;
  }
  walBuildLogName(pWal, fileFirstVer, fnameStr);
  logTfd = tfOpenReadWrite(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 78
int walChangeFile(SWal* pWal, int64_t ver) {
  int     code = 0;
L
Liu Jicong 已提交
79
  int64_t idxTfd, logTfd;
L
Liu Jicong 已提交
80
  char    fnameStr[WAL_FILE_LEN];
L
Liu Jicong 已提交
81
  code = tfClose(pWal->writeLogTfd);
L
Liu Jicong 已提交
82 83
  if (code != 0) {
    // TODO
L
Liu Jicong 已提交
84
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
85
    return -1;
L
Liu Jicong 已提交
86
  }
L
Liu Jicong 已提交
87
  code = tfClose(pWal->writeIdxTfd);
L
Liu Jicong 已提交
88
  if (code != 0) {
L
Liu Jicong 已提交
89
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
90
    return -1;
L
Liu Jicong 已提交
91
  }
L
Liu Jicong 已提交
92
  SWalFileInfo tmpInfo;
L
Liu Jicong 已提交
93
  tmpInfo.firstVer = ver;
L
Liu Jicong 已提交
94
  // bsearch in fileSet
L
Liu Jicong 已提交
95
  SWalFileInfo* pRet = taosArraySearch(pWal->fileInfoSet, &tmpInfo, compareWalFileInfo, TD_LE);
L
Liu Jicong 已提交
96
  ASSERT(pRet != NULL);
L
Liu Jicong 已提交
97
  int64_t fileFirstVer = pRet->firstVer;
L
Liu Jicong 已提交
98 99
  // closed
  if (taosArrayGetLast(pWal->fileInfoSet) != pRet) {
L
Liu Jicong 已提交
100
    walBuildIdxName(pWal, fileFirstVer, fnameStr);
L
Liu Jicong 已提交
101
    idxTfd = tfOpenRead(fnameStr);
L
Liu Jicong 已提交
102
    walBuildLogName(pWal, fileFirstVer, fnameStr);
L
Liu Jicong 已提交
103 104
    logTfd = tfOpenRead(fnameStr);
  } else {
L
Liu Jicong 已提交
105
    walBuildIdxName(pWal, fileFirstVer, fnameStr);
L
Liu Jicong 已提交
106
    idxTfd = tfOpenReadWrite(fnameStr);
L
Liu Jicong 已提交
107
    walBuildLogName(pWal, fileFirstVer, fnameStr);
L
Liu Jicong 已提交
108 109 110
    logTfd = tfOpenReadWrite(fnameStr);
  }

L
Liu Jicong 已提交
111 112
  pWal->writeLogTfd = logTfd;
  pWal->writeIdxTfd = idxTfd;
L
Liu Jicong 已提交
113
  return fileFirstVer;
L
Liu Jicong 已提交
114 115
}

L
Liu Jicong 已提交
116
int walSeekVer(SWal* pWal, int64_t ver) {
L
Liu Jicong 已提交
117
  int code;
L
Liu Jicong 已提交
118
  if (ver == pWal->vers.lastVer) {
L
Liu Jicong 已提交
119 120
    return 0;
  }
L
Liu Jicong 已提交
121
  if (ver > pWal->vers.lastVer || ver < pWal->vers.firstVer) {
L
Liu Jicong 已提交
122
    terrno = TSDB_CODE_WAL_INVALID_VER;
L
Liu Jicong 已提交
123 124
    return -1;
  }
L
Liu Jicong 已提交
125
  if (ver < pWal->vers.snapshotVer) {
L
Liu Jicong 已提交
126
  }
L
Liu Jicong 已提交
127
  if (ver < walGetCurFileFirstVer(pWal) || (ver > walGetCurFileLastVer(pWal))) {
L
Liu Jicong 已提交
128
    code = walChangeFile(pWal, ver);
L
Liu Jicong 已提交
129
    if (code != 0) {
L
Liu Jicong 已提交
130 131
      return -1;
    }
L
Liu Jicong 已提交
132
  }
L
Liu Jicong 已提交
133
  code = walSeekFilePos(pWal, ver);
L
Liu Jicong 已提交
134
  if (code != 0) {
L
Liu Jicong 已提交
135 136
    return -1;
  }
L
Liu Jicong 已提交
137

L
Liu Jicong 已提交
138 139
  return 0;
}