Verified Commit 9661533d authored by Marc Vef's avatar Marc Vef
Browse files

Update mtime bug: Added new UpdateTimeOperand for updating mtime

parent fcc80206
Loading
Loading
Loading
Loading
+32 −1
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@
#include <rocksdb/merge_operator.h>
#include <common/metadata.hpp>

#include <ctime>

namespace rdb = rocksdb;

namespace gkfs::metadata {
@@ -43,7 +45,9 @@ namespace gkfs::metadata {
enum class OperandID : char {
    increase_size = 'i',
    decrease_size = 'd',
    create = 'c'
    create = 'c',
    update_time = 't'

};

/**
@@ -156,6 +160,33 @@ public:
    std::string
    serialize_params() const override;
};

/**
 * @brief Update time operand
 */
class UpdateTimeOperand : public MergeOperand {
private:
    time_t mtime_{};

public:
    UpdateTimeOperand() = default;

    explicit UpdateTimeOperand(time_t mtime);

    explicit UpdateTimeOperand(const rdb::Slice& serialized_op);

    OperandID
    id() const override;

    std::string
    serialize_params() const override;

    time_t
    mtime() const {
        return mtime_;
    }
};

/**
 * @brief Merge operator class passed to RocksDB, used during merge operations
 */
+23 −4
Original line number Diff line number Diff line
@@ -137,6 +137,26 @@ CreateOperand::serialize_params() const {
    return metadata;
}

UpdateTimeOperand::UpdateTimeOperand(time_t mtime) : mtime_(mtime) {}

UpdateTimeOperand::UpdateTimeOperand(const rdb::Slice& serialized_op) {
    // Parse size
    size_t read = 0;
    mtime_ = ::stoul(serialized_op.ToString(), &read);
    // check that we consumed all the input string
    assert(read == serialized_op.size());
}

OperandID
UpdateTimeOperand::id() const {
    return OperandID::update_time;
}

string
UpdateTimeOperand::serialize_params() const {
    return ::to_string(mtime_);
}

/**
 * @internal
 * Merges all operands in chronological order for the same key.
@@ -181,10 +201,6 @@ MetadataMergeOperator::FullMergeV2(const MergeOperationInput& merge_in,
        auto operand_id = MergeOperand::get_id(serialized_op);
        auto parameters = MergeOperand::get_params(serialized_op);

        if constexpr(gkfs::config::metadata::use_mtime) {
            md.update_mtime_now();
        }

        if(operand_id == OperandID::increase_size) {
            auto op = IncreaseSizeOperand(parameters);
            if(op.append()) {
@@ -204,6 +220,9 @@ MetadataMergeOperator::FullMergeV2(const MergeOperationInput& merge_in,
            fsize = op.size();
        } else if(operand_id == OperandID::create) {
            continue;
        } else if(operand_id == OperandID::update_time) {
            auto op = UpdateTimeOperand(parameters);
            md.mtime(op.mtime());
        } else {
            throw ::runtime_error("Unrecognized merge operand ID: " +
                                  (char) operand_id);
+19 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@
#include <common/metadata.hpp>
#include <common/path_util.hpp>
#include <iostream>
#include <daemon/backend/metadata/rocksdb_backend.hpp>
#include <ctime>
extern "C" {
#include <sys/stat.h>
}
@@ -244,6 +244,15 @@ RocksDBBackend::increase_size_impl(const std::string& key, size_t io_size,
        if(!s.ok()) {
            throw_status_excpt(s);
        }
        if constexpr(gkfs::config::metadata::use_mtime) {
            // get current time and update mtime for this file
            time_t now = time(nullptr);
            auto t_op = UpdateTimeOperand(now);
            s = db_->Merge(write_opts_, key, t_op.serialize());
            if(!s.ok()) {
                throw_status_excpt(s);
            }
        }
    }
    return out_offset;
}
@@ -263,6 +272,15 @@ RocksDBBackend::decrease_size_impl(const std::string& key, size_t size) {
    if(!s.ok()) {
        throw_status_excpt(s);
    }
    if constexpr(gkfs::config::metadata::use_mtime) {
        // get current time and update mtime for this file
        time_t now = time(nullptr);
        auto t_op = UpdateTimeOperand(now);
        s = db_->Merge(write_opts_, key, t_op.serialize());
        if(!s.ok()) {
            throw_status_excpt(s);
        }
    }
}

/**