afs_warpper.h 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
22

23
#include "paddle/fluid/distributed/the_one_ps.pb.h"
24
#include "paddle/fluid/string/string_helper.h"
25
#include "paddle/phi/core/macros.h"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
namespace paddle {
namespace distributed {
struct FsDataConverter {
  std::string converter;
  std::string deconverter;
};

struct FsChannelConfig {
  std::string path;       // path of file
  std::string converter;  // data converter
  std::string deconverter;
};

class FsReadChannel {
 public:
  FsReadChannel() : _buffer_size(0) {}
  explicit FsReadChannel(uint32_t buffer_size) : _buffer_size(buffer_size) {}
  virtual ~FsReadChannel() {}
  FsReadChannel(FsReadChannel&&) = delete;
  FsReadChannel(const FsReadChannel&) = delete;
46
  int open(std::shared_ptr<FILE> fp, const FsChannelConfig& config UNUSED) {
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    _file = fp;
    return 0;
  }
  inline int close() {
    _file.reset();
    return 0;
  }

  inline uint32_t read_line(std::string& line_data) {  // NOLINT
    line_data.clear();
    char buffer = '\0';
    size_t read_count = 0;
    while (1 == fread(&buffer, 1, 1, _file.get()) && buffer != '\n') {
      ++read_count;
      line_data.append(&buffer, 1);
    }
    if (read_count == 0 && buffer != '\n') {
      return -1;
    }
    return 0;
  }

L
lxsbupt 已提交
69 70 71 72
  inline int read(char* data, size_t size) {
    return fread(data, 1, size, _file.get());
  }

73 74 75 76 77 78 79 80 81 82 83 84 85
 private:
  uint32_t _buffer_size;
  FsChannelConfig _config;
  std::shared_ptr<FILE> _file;
};
class FsWriteChannel {
 public:
  FsWriteChannel() : _buffer_size(0) {}
  explicit FsWriteChannel(uint32_t buffer_size) : _buffer_size(buffer_size) {}
  virtual ~FsWriteChannel() {}
  FsWriteChannel(FsWriteChannel&&) = delete;
  FsWriteChannel(const FsWriteChannel&) = delete;

86
  int open(std::shared_ptr<FILE> fp, const FsChannelConfig& config UNUSED) {
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    _file = fp;

    // the buffer has set in fs.cc
    // if (_buffer_size != 0) {
    //    _buffer = std::shared_ptr<char>(new char[_buffer_size]);

    //    CHECK(0 == setvbuf(&*_file, _buffer.get(), _IOFBF, _buffer_size));
    //}
    return 0;
  }

  inline void flush() { return; }

  inline int close() {
    flush();
    _file.reset();
    return 0;
  }

  inline uint32_t write_line(const char* data, uint32_t size) {
    size_t write_count = fwrite_unlocked(data, 1, size, _file.get());
    if (write_count != size) {
      return -1;
    }
    write_count = fwrite_unlocked("\n", 1, 1, _file.get());
    if (write_count != 1) {
      return -1;
    }
    return 0;
  }
  inline uint32_t write_line(const std::string& data) {
    return write_line(data.c_str(), data.size());
  }

L
lxsbupt 已提交
121 122 123 124 125 126 127 128
  inline uint32_t write(const char* data, size_t size) {
    size_t write_count = fwrite(data, 1, size, _file.get());
    if (write_count != size) {
      return -1;
    }
    return 0;
  }

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
 private:
  uint32_t _buffer_size;
  FsChannelConfig _config;
  std::shared_ptr<FILE> _file;
  std::shared_ptr<char> _buffer;
};

class AfsClient {
 public:
  AfsClient() {}
  virtual ~AfsClient() {}
  AfsClient(AfsClient&&) = delete;
  AfsClient(const AfsClient&) = delete;

  int initialize(const FsClientParameter& fs_client_param);
144 145 146 147 148 149 150 151
  int initialize(const std::string& hadoop_bin,
                 const std::string& uri,
                 const std::string& user,
                 const std::string& passwd,
                 int buffer_size_param = (1L << 25));
  int initialize(const std::string& hadoop_bin,
                 const std::string& uri,
                 const std::string& ugi,
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
                 int buffer_size_param = (1L << 25));

  // open file in 'w' or 'r'
  std::shared_ptr<FsReadChannel> open_r(const FsChannelConfig& config,
                                        uint32_t buffer_size = 0,
                                        int* err_no = nullptr);
  std::shared_ptr<FsWriteChannel> open_w(const FsChannelConfig& config,
                                         uint32_t buffer_size = 0,
                                         int* err_no = nullptr);

  // remove file in path, path maybe a reg, such as 'part-000-*'
  void remove(const std::string& path);
  void remove_dir(const std::string& dir);

  // list files in path, path maybe a dir with reg
  std::vector<std::string> list(const std::string& path);

  // exist or not
  bool exist(const std::string& dir);
};
}  // namespace distributed
}  // namespace paddle