diff --git a/Makefile b/Makefile index b1894115..044519bc 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,7 @@ OBJS = \ src/encrypted_storage_writer.o src/io_adv.o src/io.o src/crypto.o \ src/x_reader.o src/x_writer.o \ src/virtual_index.o \ + src/metadata_relations.o \ src/walg_reader.o \ src/walg_writer.o \ src/walg_st_reader.o \ diff --git a/include/metadata.h b/include/metadata.h new file mode 100644 index 00000000..5959c3f0 --- /dev/null +++ b/include/metadata.h @@ -0,0 +1,14 @@ +#ifndef YEZZEY_METADATA_H +#define YEZZEY_METADATA_H + +#ifdef __cplusplus +#define EXTERNC extern "C" +#else +#define EXTERNC +#endif + +// XXX: todo proder interface + +EXTERNC void YezzeyCreateMetadataRelations(); + +#endif /* YEZZEY_METADATA_H */ \ No newline at end of file diff --git a/include/offload_policy.h b/include/offload_policy.h index 4ab2050c..81896579 100644 --- a/include/offload_policy.h +++ b/include/offload_policy.h @@ -44,7 +44,7 @@ typedef FormData_yezzey_offload_metadata *Form_yezzey_offload_metadata; #define Offload_policy_always_remote 1 #define Offload_policy_cache_writes 2 -EXTERNC void YezzeyOffloadPolicyRelation(); +EXTERNC void YezzeyCreateOffloadMetadataRelation(); EXTERNC void YezzeySetRelationExpiritySeg(Oid i_reloid /* offload relation oid */, diff --git a/include/virtual_index.h b/include/virtual_index.h index a46dcd5a..a7cf76e4 100644 --- a/include/virtual_index.h +++ b/include/virtual_index.h @@ -22,41 +22,47 @@ * ---------------- */ +#define YEZZEY_VIRTUAL_INDEX_RELATION 8602 + +#define YEZZEY_VIRTUAL_INDEX_RELATION_INDX 8603 + +#define YEZZEY_CHUNK_STATUS_ACTIVE 1 +#define YEZZEY_CHUNK_STATUS_BLOAT 2 +#define YEZZEY_CHUNK_STATUS_OBSOLETE 3 + typedef struct { int segno; /* AO relation block file no */ int64_t start_offset; /* start_offset of block file chunk */ int64_t finish_offset; /* finish_offset of block file chunk */ int64_t modcount; /* modcount of block file chunk */ XLogRecPtr lsn; /* Chunk lsn */ + int status; /* Chunk status (see YEZZEY_CHUNK_STATUS_* above) */ text x_path; /* external path */ } FormData_yezzey_virtual_index; -#define Natts_yezzey_virtual_index 7 -#define Anum_yezzey_virtual_index_segno 1 +/* we dont need relation oid for chucks index */ +#define Natts_yezzey_virtual_index 8 +#define Anum_yezzey_virtual_index_blkno 1 #define Anum_yezzey_virtual_index_filenode 2 #define Anum_yezzey_virtual_start_off 3 #define Anum_yezzey_virtual_finish_off 4 #define Anum_yezzey_virtual_modcount 5 #define Anum_yezzey_virtual_lsn 6 -#define Anum_yezzey_virtual_x_path 7 - -EXTERNC Oid YezzeyCreateAuxIndex(Relation aorel); +#define Anum_yezzey_virtual_status 7 +#define Anum_yezzey_virtual_x_path 8 -EXTERNC Oid YezzeyFindAuxIndex(Oid reloid); +EXTERNC void YezzeyCreateAuxIndex(void); -EXTERNC void emptyYezzeyIndex(Oid yezzey_index_oid); +EXTERNC void emptyYezzeyIndex(Oid relfilenode); -EXTERNC void emptyYezzeyIndexBlkno(Oid yezzey_index_oid, int blkno, - Oid relfilenode); +EXTERNC void emptyYezzeyIndexBlkno(int blkno, Oid relfilenode); #ifdef __cplusplus -void YezzeyVirtualIndexInsert(Oid yandexoid /*yezzey auxiliary index oid*/, - int64_t segindx, Oid relfilenodeOid, +void YezzeyVirtualIndexInsert(int64_t blkno, Oid relfilenodeOid, + int64_t offset_start, int64_t offset_finish, int64_t modcount, XLogRecPtr lsn, const char *x_path /* external path */); -std::vector -YezzeyVirtualGetOrder(Oid yandexoid /*yezzey auxiliary index oid*/, int blkno, - Oid relfilenode); +std::vector YezzeyVirtualGetOrder(int blkno, Oid relfilenode); #else #endif diff --git a/smgr.c b/smgr.c index 99f7a2de..22ddee76 100644 --- a/smgr.c +++ b/smgr.c @@ -36,6 +36,7 @@ #include "storage.h" +#include "virtual_index.h" #include "proxy.h" /* @@ -129,25 +130,6 @@ bool yezzey_exists(SMgrRelation reln, ForkNumber forkNum) { return mdexists(reln, forkNum); } -void -#ifndef GPBUILD -yezzey_unlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo) -#else -yezzey_unlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo, char relstorage) -#endif -{ - if (rnode.node.spcNode == YEZZEYTABLESPACE_OID) { - /*do nothing */ - return; - } - -#ifndef GPBUILD - mdunlink(rnode, forkNum, isRedo); -#else - mdunlink(rnode, forkNum, isRedo, relstorage); -#endif -} - void yezzey_extend(SMgrRelation reln, ForkNumber forkNum, BlockNumber blockNum, char *buffer, bool skipFsync) { if ((reln->smgr_rnode).node.spcNode == YEZZEYTABLESPACE_OID) { @@ -236,6 +218,31 @@ void yezzey_immedsync(SMgrRelation reln, ForkNumber forkNum) { mdimmedsync(reln, forkNum); } + +#ifndef GPBUILD +void yezzey_unlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo) +#else +void yezzey_unlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo, char relstorage) +#endif +{ + if (rnode.node.spcNode == YEZZEYTABLESPACE_OID) { + /* clear relfilenode from its internal index */ + + /* Do it only on QE? */ + if (Gp_role == GP_ROLE_EXECUTE) { + (void)emptyYezzeyIndex(rnode.node.relNode); + } + return; + } + +#ifndef GPBUILD + mdunlink(rnode, forkNum, isRedo); +#else + mdunlink(rnode, forkNum, isRedo, relstorage); +#endif +} + + static const struct f_smgr yezzey_smgr = { .smgr_init = yezzey_init, .smgr_shutdown = NULL, diff --git a/src/encrypted_storage_reader.cpp b/src/encrypted_storage_reader.cpp index 3dffc585..c530613e 100644 --- a/src/encrypted_storage_reader.cpp +++ b/src/encrypted_storage_reader.cpp @@ -6,7 +6,7 @@ EncryptedStorageReader::EncryptedStorageReader( std::shared_ptr adv, const std::vector &order, ssize_t segindx) : adv_(adv), order_(order), segindx_(segindx) { - buf_ = std::make_shared(1 << 12); + buf_ = std::make_shared(1 << 16); /* TODO: with order */ reader_ = std::make_shared(adv_, order_, segindx_); diff --git a/src/io.cpp b/src/io.cpp index 936f3e14..7c00a8b7 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -22,8 +22,7 @@ YIO::YIO(std::shared_ptr adv, ssize_t segindx, ssize_t modcount, const std::string &storage_path) : adv_(adv), segindx_(segindx), modcount_(modcount), - order_(YezzeyVirtualGetOrder(YezzeyFindAuxIndex(adv->reloid), - adv->coords_.blkno, adv->coords_.filenode)) { + order_(YezzeyVirtualGetOrder(adv->coords_.blkno, adv->coords_.filenode)) { #if USE_WLG_READER reader_ = std::make_shared(adv_, segindx_, order_); @@ -43,8 +42,7 @@ YIO::YIO(std::shared_ptr adv, ssize_t segindx, ssize_t modcount, YIO::YIO(std::shared_ptr adv, ssize_t segindx) : adv_(adv), segindx_(segindx), - order_(YezzeyVirtualGetOrder(YezzeyFindAuxIndex(adv->reloid), - adv->coords_.blkno, adv->coords_.filenode)) { + order_(YezzeyVirtualGetOrder(adv->coords_.blkno, adv->coords_.filenode)) { #if USE_WLG_READER reader_ = std::make_shared(adv_, segindx_, order_); diff --git a/src/metadata_relations.cpp b/src/metadata_relations.cpp new file mode 100644 index 00000000..f767cb1c --- /dev/null +++ b/src/metadata_relations.cpp @@ -0,0 +1,12 @@ +#include "metadata.h" +#include "offload_policy.h" +#include "virtual_index.h" + +void YezzeyCreateMetadataRelations() { + /* if any error, elog(ERROR, ...) is called + * and due to magic of MVCC everything is rolled back + * and no additional job needed. + */ + (void)YezzeyCreateOffloadMetadataRelation(); + (void)YezzeyCreateAuxIndex(); +} \ No newline at end of file diff --git a/src/offload_policy.cpp b/src/offload_policy.cpp index 632bf1af..27418eb1 100644 --- a/src/offload_policy.cpp +++ b/src/offload_policy.cpp @@ -15,7 +15,7 @@ DISTRIBUTED REPLICATED; const std::string offload_metadata_relname = "offload_metadata"; const std::string offload_metadata_relname_indx = "offload_metadata_indx"; -void YezzeyOffloadPolicyRelation() { +void YezzeyCreateOffloadMetadataRelation() { { /* check existed, if no, return */ } TupleDesc tupdesc; @@ -207,8 +207,6 @@ void YezzeyDefineOffloadPolicy(Oid reloid) { auto aorel = relation_open(reloid, AccessExclusiveLock); RelationOpenSmgr(aorel); - (void)YezzeyCreateAuxIndex(aorel); - /* * @brief do main offload job on segments * diff --git a/src/proxy.cpp b/src/proxy.cpp index 5bec26e0..441f9ac0 100644 --- a/src/proxy.cpp +++ b/src/proxy.cpp @@ -12,6 +12,9 @@ #include "io.h" #include "io_adv.h" +#include "pg.h" +#include "storage/md.h" + typedef struct YVirtFD { int y_vfd; /* Either YEZZEY_* preserved fd or pg internal fd >= 9 */ @@ -33,6 +36,10 @@ typedef struct YVirtFD { int64 reloid; int64 offset; + int64 op_start_offset; + /* unneede, because this offset is equal to total offset and moment of + * FileClose */ + // int64 op_end_offset; int64 virtualSize; int64 modcount; @@ -44,8 +51,8 @@ typedef struct YVirtFD { YVirtFD() : y_vfd(-1), localTmpVfd(0), handler(nullptr), fileFlags(0), fileMode(0), - reloid(InvalidOid), offset(0), virtualSize(0), modcount(0), op_write(0), - offloaded(false) {} + reloid(InvalidOid), offset(0), op_start_offset(0), virtualSize(0), + modcount(0), op_write(0), offloaded(false) {} YVirtFD &operator=(YVirtFD &&vfd) { handler = std::move(vfd.handler); @@ -120,7 +127,10 @@ int64 yezzey_FileSeek(SMGRFile file, int64 offset, int whence) { File actual_fd = YVirtFD_cache[file].y_vfd; if (actual_fd == YEZZEY_OFFLOADED_FD) { // what? + /* TDB: check that offset == max_offset from metadata table */ YVirtFD_cache[file].offset = offset; + /* TDB: check sanity of this operation */ + YVirtFD_cache[file].op_start_offset = offset; return offset; } elog(yezzey_ao_log_level, @@ -263,12 +273,12 @@ void yezzey_FileClose(SMGRFile file) { /* record file only if non-zero bytes was stored */ if (yfd.op_write) { /* insert entry in yezzey index */ - YezzeyVirtualIndexInsert(YezzeyFindAuxIndex(yfd.reloid), - yfd.coord.blkno /* blkno*/, yfd.coord.filenode, - yfd.modcount, - yfd.handler->writer_->getInsertionStorageLsn(), - yfd.handler->writer_->getExternalStoragePath() - .c_str() /* path ? */); + YezzeyVirtualIndexInsert( + yfd.coord.blkno /* blkno*/, yfd.coord.filenode, yfd.op_start_offset, + yfd.offset /* io operation finish offset */, yfd.modcount, + yfd.handler->writer_->getInsertionStorageLsn(), + yfd.handler->writer_->getExternalStoragePath() + .c_str() /* path ? */); } } else { @@ -385,12 +395,11 @@ int yezzey_FileTruncate(SMGRFile yezzey_fd, int64 offset) { /* Do it only on QE? */ if (Gp_role == GP_ROLE_EXECUTE) { - (void)emptyYezzeyIndexBlkno(YezzeyFindAuxIndex(yfd.reloid), - yfd.handler->adv_->coords_.blkno, + (void)emptyYezzeyIndexBlkno(yfd.handler->adv_->coords_.blkno, yfd.handler->adv_->coords_.filenode); } } return 0; } return FileTruncate(actual_fd, offset); -} +} \ No newline at end of file diff --git a/src/storage.cpp b/src/storage.cpp index 5b45de0f..c83b1027 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -50,7 +50,6 @@ int offloadRelationSegmentPath(Relation aorel, const std::string &nspname, size_t chunkSize = 1 << 20; File vfd; int64 curr_read_chunk; - int64 progress; int64 virtual_size; std::vector buffer(chunkSize); @@ -60,7 +59,6 @@ int offloadRelationSegmentPath(Relation aorel, const std::string &nspname, "yezzey: failed to open %s file to transfer to external storage", localPath.c_str()); } - progress = 0; auto ioadv = std::make_shared( gpg_engine_path, gpg_key_id, storage_config, nspname, relname, @@ -84,17 +82,11 @@ int offloadRelationSegmentPath(Relation aorel, const std::string &nspname, } elog(NOTICE, "yezzey: relation virtual size calculated: %ld", virtual_size); - progress = virtual_size; + auto progress = virtual_size; + auto offset_start = progress; FileSeek(vfd, progress, SEEK_SET); rc = 0; - /* insert chunk metadata in virtual index */ - YezzeyVirtualIndexInsert( - YezzeyFindAuxIndex(aorel->rd_id), ioadv->coords_.blkno /* blkno*/, - ioadv->coords_.filenode, modcount, - iohandler.writer_->getInsertionStorageLsn(), - iohandler.writer_->getExternalStoragePath().c_str() /* path */); - while (progress < logicalEof) { curr_read_chunk = chunkSize; if (progress + curr_read_chunk > logicalEof) { @@ -128,6 +120,15 @@ int offloadRelationSegmentPath(Relation aorel, const std::string &nspname, progress += rc; } + auto offset_finish = progress; + + /* data persisted in external storage, we can update out metadata relations */ + /* insert chunk metadata in virtual index */ + YezzeyVirtualIndexInsert( + ioadv->coords_.blkno /* blkno*/, ioadv->coords_.filenode, offset_start, + offset_finish, modcount, iohandler.writer_->getInsertionStorageLsn(), + iohandler.writer_->getExternalStoragePath().c_str() /* path */); + if (!iohandler.io_close()) { elog(ERROR, "yezzey: failed to complete %s offloading", localPath.c_str()); } diff --git a/src/virtual_index.cpp b/src/virtual_index.cpp index 731481dd..2ea0dc99 100644 --- a/src/virtual_index.cpp +++ b/src/virtual_index.cpp @@ -3,23 +3,17 @@ #include -Oid YezzeyFindAuxIndex_internal(Oid reloid); - -Oid YezzeyCreateAuxIndex(Relation aorel) { - { - auto tmp = YezzeyFindAuxIndex_internal(RelationGetRelid(aorel)); - if (OidIsValid(tmp)) { - return tmp; - } - } +const std::string aux_index_relname = "yezzey_virtual_index"; +const std::string aux_index_relname_indx = "yezzey_virtual_indexindx"; +void YezzeyCreateAuxIndex(void) { ObjectAddress baseobject; ObjectAddress yezzey_ao_auxiliaryobject; auto tupdesc = CreateTemplateTupleDesc(Natts_yezzey_virtual_index, false); - TupleDescInitEntry(tupdesc, (AttrNumber)Anum_yezzey_virtual_index_segno, - "segno", INT4OID, -1, 0); + TupleDescInitEntry(tupdesc, (AttrNumber)Anum_yezzey_virtual_index_blkno, + "blkno", INT4OID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber)Anum_yezzey_virtual_index_filenode, "filenode", OIDOID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber)Anum_yezzey_virtual_start_off, @@ -30,36 +24,88 @@ Oid YezzeyCreateAuxIndex(Relation aorel) { "modcount", INT8OID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber)Anum_yezzey_virtual_lsn, "lsn", LSNOID, -1, 0); + + TupleDescInitEntry(tupdesc, (AttrNumber)Anum_yezzey_virtual_status, "status", + INT4OID, -1, 0); + TupleDescInitEntry(tupdesc, (AttrNumber)Anum_yezzey_virtual_x_path, "x_path", TEXTOID, -1, 0); - auto yezzey_ao_auxiliary_relname = std::string("yezzey_virtual_index") + - std::to_string(RelationGetRelid(aorel)); - - auto yezzey_ao_auxiliary_relid = heap_create_with_catalog( - yezzey_ao_auxiliary_relname.c_str() /* relname */, + (void)heap_create_with_catalog( + aux_index_relname.c_str() /* relname */, YEZZEY_AUX_NAMESPACE /* namespace */, 0 /* tablespace */, - GetNewObjectId() /* relid */, GetNewObjectId() /* reltype oid */, - InvalidOid /* reloftypeid */, aorel->rd_rel->relowner /* owner */, - tupdesc /* rel tuple */, NIL, InvalidOid /* relam */, RELKIND_YEZZEYINDEX, - aorel->rd_rel->relpersistence, RELSTORAGE_HEAP, - aorel->rd_rel->relisshared, RelationIsMapped(aorel), true, 0, - ONCOMMIT_NOOP, NULL /* GP Policy */, (Datum)0, false /* use_user_acl */, - true, true, false /* valid_opts */, false /* is_part_child */, - false /* is part parent */); + YEZZEY_VIRTUAL_INDEX_RELATION /* relid */, + GetNewObjectId() /* reltype oid */, InvalidOid /* reloftypeid */, + GetUserId() /* owner */, tupdesc /* rel tuple */, NIL, + InvalidOid /* relam */, RELKIND_RELATION, RELPERSISTENCE_PERMANENT, + RELSTORAGE_HEAP, false /* is shared */, false /* rel is mapped */, true, + 0, ONCOMMIT_NOOP, NULL /* GP Policy */, (Datum)0, + false /* use_user_acl */, true, true, false /* valid_opts */, + false /* is_part_child */, false /* is part parent */); /* Make this table visible, else yezzey virtual index creation will fail */ CommandCounterIncrement(); + // #define YEZZEY_VIRT_INDEX_COLUMNS 2 + + // /* ShareLock is not really needed here, but take it anyway */ + // auto yezzey_virt_indx_rel = + // heap_open(YEZZEY_VIRTUAL_INDEX_RELATION, ShareLock); + // char *colnameFirst = "blkno"; + // char *colnameSecond = "filenode"; + // auto indexColNames = + // list_make2(makeString(colnameFirst), makeString(colnameSecond)); + + // auto indexInfo = makeNode(IndexInfo); + + // Oid collationObjectId[YEZZEY_VIRT_INDEX_COLUMNS]; + // Oid classObjectId[YEZZEY_VIRT_INDEX_COLUMNS]; + // int16 coloptions[YEZZEY_VIRT_INDEX_COLUMNS]; + + // indexInfo->ii_NumIndexAttrs = 2; + // indexInfo->ii_KeyAttrNumbers[0] = Anum_yezzey_virtual_index_blkno; + // indexInfo->ii_KeyAttrNumbers[1] = Anum_yezzey_virtual_index_filenode; + // indexInfo->ii_Expressions = NIL; + // indexInfo->ii_ExpressionsState = NIL; + // indexInfo->ii_Predicate = NIL; + // indexInfo->ii_PredicateState = NIL; + // indexInfo->ii_Unique = true; + // indexInfo->ii_Concurrent = false; + + // collationObjectId[0] = InvalidOid; + // collationObjectId[1] = InvalidOid; + + // classObjectId[0] = INT4_BTREE_OPS_OID; + // classObjectId[1] = OID_BTREE_OPS_OID; + + // coloptions[0] = 0; + // coloptions[1] = 0; + + // (void)index_create(yezzey_virt_indx_rel, aux_index_relname_indx.c_str(), + // YEZZEY_VIRTUAL_INDEX_RELATION_INDX, InvalidOid, + // InvalidOid, InvalidOid, indexInfo, indexColNames, + // BTREE_AM_OID, 0 /* tablespace */, collationObjectId, + // classObjectId, coloptions, (Datum)0, true, false, + // false, false, true, false, false, true, NULL); + + // /* Unlock target table -- no one can see it */ + // heap_close(yezzey_virt_indx_rel, ShareLock); + + // /* Unlock the index -- no one can see it anyway */ + // UnlockRelationOid(YEZZEY_VIRTUAL_INDEX_RELATION_INDX, + // AccessExclusiveLock); + + // CommandCounterIncrement(); + /* * Register dependency from the auxiliary table to the master, so that the * aoseg table will be deleted if the master is. */ - baseobject.classId = RelationRelationId; - baseobject.objectId = RelationGetRelid(aorel); + baseobject.classId = ExtensionRelationId; + baseobject.objectId = get_extension_oid("yezzey", false); baseobject.objectSubId = 0; yezzey_ao_auxiliaryobject.classId = RelationRelationId; - yezzey_ao_auxiliaryobject.objectId = yezzey_ao_auxiliary_relid; + yezzey_ao_auxiliaryobject.objectId = YEZZEY_VIRTUAL_INDEX_RELATION; yezzey_ao_auxiliaryobject.objectSubId = 0; recordDependencyOn(&yezzey_ao_auxiliaryobject, &baseobject, @@ -69,62 +115,22 @@ Oid YezzeyCreateAuxIndex(Relation aorel) { * Make changes visible */ CommandCounterIncrement(); - - return yezzey_ao_auxiliary_relid; -} - -Oid YezzeyFindAuxIndex_internal(Oid reloid) { - HeapTuple tup; - ScanKeyData skey[2]; - - auto yezzey_virtual_index_oid = InvalidOid; - - auto yezzey_ao_auxiliary_relname = - std::string("yezzey_virtual_index") + std::to_string(reloid); - - /* - * Check the pg_appendonly relation to be certain the ao table - * is there. - */ - auto pg_class = heap_open(RelationRelationId, AccessShareLock); - - ScanKeyInit(&skey[0], Anum_pg_class_relname, BTEqualStrategyNumber, F_NAMEEQ, - CStringGetDatum(yezzey_ao_auxiliary_relname.c_str())); - - ScanKeyInit(&skey[1], Anum_pg_class_relnamespace, BTEqualStrategyNumber, - F_OIDEQ, ObjectIdGetDatum(YEZZEY_AUX_NAMESPACE)); - - auto scan = - systable_beginscan(pg_class, ClassNameNspIndexId, true, NULL, 2, skey); - - if (HeapTupleIsValid(tup = systable_getnext(scan))) { - yezzey_virtual_index_oid = HeapTupleGetOid(tup); - } - - systable_endscan(scan); - heap_close(pg_class, AccessShareLock); - - return yezzey_virtual_index_oid; -} - -Oid YezzeyFindAuxIndex(Oid reloid) { - Oid yezzey_virtual_index_oid = YezzeyFindAuxIndex_internal(reloid); - if (OidIsValid(yezzey_virtual_index_oid)) { - return yezzey_virtual_index_oid; - } - elog(ERROR, "could not find yezzey virtual index oid for relation \"%d\"", - reloid); } -void emptyYezzeyIndex(Oid yezzey_index_oid) { +void emptyYezzeyIndex(Oid relfilenode) { HeapTuple tuple; - /* DELETE FROM yezzey.yezzey_virtual_index_ */ - auto rel = heap_open(yezzey_index_oid, RowExclusiveLock); + ScanKeyData skey[1]; + + /* DELETE FROM yezzey.yezzey_virtual_index WHERE relfilenode = */ + auto rel = heap_open(YEZZEY_VIRTUAL_INDEX_RELATION, RowExclusiveLock); auto snap = RegisterSnapshot(GetTransactionSnapshot()); - auto desc = heap_beginscan(rel, snap, 0, NULL); + ScanKeyInit(&skey[0], Anum_yezzey_virtual_index_filenode, + BTEqualStrategyNumber, F_OIDEQ, ObjectIdGetDatum(relfilenode)); + + auto desc = heap_beginscan(rel, snap, 1, skey); while (HeapTupleIsValid(tuple = heap_getnext(desc, ForwardScanDirection))) { simple_heap_delete(rel, &tuple->t_self); @@ -142,21 +148,21 @@ void emptyYezzeyIndex(Oid yezzey_index_oid) { /* Update this settings if where clause expr changes */ #define YezzeyVirtualIndexScanCols 2 -void emptyYezzeyIndexBlkno(Oid yezzey_index_oid, int blkno, Oid relfilenode) { +void emptyYezzeyIndexBlkno(int blkno, Oid relfilenode) { HeapTuple tuple; ScanKeyData skey[YezzeyVirtualIndexScanCols]; /* DELETE FROM yezzey.yezzey_virtual_index_ WHERE segno = */ - auto rel = heap_open(yezzey_index_oid, RowExclusiveLock); + auto rel = heap_open(YEZZEY_VIRTUAL_INDEX_RELATION, RowExclusiveLock); auto snap = RegisterSnapshot(GetTransactionSnapshot()); - ScanKeyInit(&skey[0], Anum_yezzey_virtual_index_segno, BTEqualStrategyNumber, + ScanKeyInit(&skey[0], Anum_yezzey_virtual_index_blkno, BTEqualStrategyNumber, F_INT4EQ, Int32GetDatum(blkno)); ScanKeyInit(&skey[1], Anum_yezzey_virtual_index_filenode, - BTEqualStrategyNumber, F_OIDEQ, ObjectIdGetDatum(blkno)); + BTEqualStrategyNumber, F_OIDEQ, ObjectIdGetDatum(relfilenode)); auto desc = heap_beginscan(rel, snap, YezzeyVirtualIndexScanCols, skey); @@ -173,8 +179,8 @@ void emptyYezzeyIndexBlkno(Oid yezzey_index_oid, int blkno, Oid relfilenode) { CommandCounterIncrement(); } /* end emptyYezzeyIndexBlkno */ -void YezzeyVirtualIndexInsert(Oid yandexoid /*yezzey auxiliary index oid*/, - int64_t segindx, Oid relfilenodeOid, +void YezzeyVirtualIndexInsert(int64_t blkno, Oid relfilenodeOid, + int64_t offset_start, int64_t offset_finish, int64_t modcount, XLogRecPtr lsn, const char *x_path /* external path */) { bool nulls[Natts_yezzey_virtual_index]; @@ -183,18 +189,20 @@ void YezzeyVirtualIndexInsert(Oid yandexoid /*yezzey auxiliary index oid*/, memset(nulls, 0, sizeof(nulls)); memset(values, 0, sizeof(values)); - /* INSERT INTO yezzey.yezzey_virtual_index_ VALUES(segno, start_offset, + /* INSERT INTO yezzey.yezzey_virtual_index VALUES(segno, start_offset, * 0, modcount, external_path) */ - auto yandxrel = heap_open(yandexoid, RowExclusiveLock); + auto yandxrel = heap_open(YEZZEY_VIRTUAL_INDEX_RELATION, RowExclusiveLock); - values[Anum_yezzey_virtual_index_segno - 1] = Int64GetDatum(segindx); + values[Anum_yezzey_virtual_index_blkno - 1] = Int64GetDatum(blkno); values[Anum_yezzey_virtual_index_filenode - 1] = ObjectIdGetDatum(relfilenodeOid); - values[Anum_yezzey_virtual_start_off - 1] = Int64GetDatum(0); - values[Anum_yezzey_virtual_finish_off - 1] = Int64GetDatum(0); + values[Anum_yezzey_virtual_start_off - 1] = Int64GetDatum(offset_start); + values[Anum_yezzey_virtual_finish_off - 1] = Int64GetDatum(offset_finish); values[Anum_yezzey_virtual_modcount - 1] = Int64GetDatum(modcount); values[Anum_yezzey_virtual_lsn - 1] = LSNGetDatum(lsn); + values[Anum_yezzey_virtual_status - 1] = + Int32GetDatum(YEZZEY_CHUNK_STATUS_ACTIVE); values[Anum_yezzey_virtual_x_path - 1] = PointerGetDatum(cstring_to_text(x_path)); @@ -209,9 +217,7 @@ void YezzeyVirtualIndexInsert(Oid yandexoid /*yezzey auxiliary index oid*/, CommandCounterIncrement(); } -std::vector -YezzeyVirtualGetOrder(Oid yandexoid /*yezzey auxiliary index oid*/, int blkno, - Oid relfilenode) { +std::vector YezzeyVirtualGetOrder(int blkno, Oid relfilenode) { /* SELECT external_path * FROM yezzey.yezzey_virtual_index_ @@ -223,11 +229,11 @@ YezzeyVirtualGetOrder(Oid yandexoid /*yezzey auxiliary index oid*/, int blkno, std::vector res; - auto rel = heap_open(yandexoid, RowExclusiveLock); + auto rel = heap_open(YEZZEY_VIRTUAL_INDEX_RELATION, RowExclusiveLock); auto snap = RegisterSnapshot(GetTransactionSnapshot()); - ScanKeyInit(&skey[0], Anum_yezzey_virtual_index_segno, BTEqualStrategyNumber, + ScanKeyInit(&skey[0], Anum_yezzey_virtual_index_blkno, BTEqualStrategyNumber, F_INT4EQ, Int32GetDatum(blkno)); ScanKeyInit(&skey[1], Anum_yezzey_virtual_index_filenode, diff --git a/src/walg_st_reader.cpp b/src/walg_st_reader.cpp index 035c218d..fbff5746 100644 --- a/src/walg_st_reader.cpp +++ b/src/walg_st_reader.cpp @@ -54,7 +54,6 @@ bool WALGSTReader::read(char *buffer, size_t *amount) { wal_g_->read(buffer, *amount); *amount = wal_g_->gcount(); - std::cerr << cmd_ << " read " << *amount << "\n"; return *amount > 0; } int WALGSTReader::prepare() { return 0; } diff --git a/test/regress/simple.sql b/test/regress/simple.sql index 4281836c..af7a2c90 100644 --- a/test/regress/simple.sql +++ b/test/regress/simple.sql @@ -19,5 +19,10 @@ SELECT count(1) FROM regaoty; SELECT * FROM regaoty LIMIT 5 OFFSET 7823; + +SELECT * FROM yezzey_offload_relation_status('regaoty'); +SELECT * FROM yezzey_offload_relation_status_per_filesegment('regaoty'); +SELECT * FROM yezzey_relation_describe_external_storage_structure('regaoty'); + \c postgres DROP DATABASE testreg; diff --git a/yezzey--1.0.sql b/yezzey--1.0.sql index c2a2452d..522b0e89 100644 --- a/yezzey--1.0.sql +++ b/yezzey--1.0.sql @@ -548,13 +548,13 @@ $$ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION yezzey_dump_virtual_index(i_relname text) -RETURNS TABLE(segno integer, relfilenode OID, offset_start bigint, offset_finish bigint, modcount bigint, lsn pg_lsn, x_path TEXT) +RETURNS TABLE(blkno integer, relfilenode OID, offset_start bigint, offset_finish bigint, modcount bigint, lsn pg_lsn, status integer, x_path TEXT) AS $$ DECLARE v_reloid OID; BEGIN select oid from pg_class INTO v_reloid where relname = i_relname; - RETURN QUERY EXECUTE 'SELECT * FROM yezzey.yezzey_virtual_index'||v_reloid||';'; + RETURN QUERY SELECT * FROM yezzey.yezzey_virtual_index; END; $$ EXECUTE ON ALL SEGMENTS diff --git a/yezzey.c b/yezzey.c index e2a6ff04..abdccb85 100644 --- a/yezzey.c +++ b/yezzey.c @@ -51,6 +51,7 @@ #include "storage.h" #include "util.h" #include "virtual_index.h" +#include "metadata.h" #include "catalog/oid_dispatch.h" @@ -105,7 +106,7 @@ PG_FUNCTION_INFO_V1(yezzey_check_part_exr); void yezzey_log_smgroffload(RelFileNode *rnode); Datum yezzey_init_metadata(PG_FUNCTION_ARGS) { - (void)YezzeyOffloadPolicyRelation(); + (void)YezzeyCreateMetadataRelations(); PG_RETURN_VOID(); } @@ -239,7 +240,7 @@ int yezzey_load_relation_internal(Oid reloid, const char *dest_path) { */ /* empty all track info */ - (void)emptyYezzeyIndex(YezzeyFindAuxIndex(reloid)); + (void)emptyYezzeyIndex(aorel->rd_node.relNode /* relfilenode */); /* cleanup */ diff --git a/yezzey.h b/yezzey.h index 4c133d55..4f5b66f2 100644 --- a/yezzey.h +++ b/yezzey.h @@ -44,12 +44,14 @@ void yezzey_create_ao(RelFileNodeBackend rnode, int32 segmentFileNum, bool isRedo); #endif bool yezzey_exists(SMgrRelation reln, ForkNumber forkNum); + #ifndef GPBUILD void yezzey_unlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo); #else void yezzey_unlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo, - char relstorage); + char relstorage); #endif + void yezzey_extend(SMgrRelation reln, ForkNumber forkNum, BlockNumber blockNum, char *buffer, bool skipFsync); #if PG_VERSION_NUM >= 130000