MemBlock.hpp 5.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/******************************************************************************
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 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.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * 
 *
 * @author Castor Dev team, castor-dev@cern.ch
 *****************************************************************************/

#pragma once

26
#include "castor/tape/tapeserver/exception/Exception.hpp"
27
#include "castor/tape/tapeserver/file/File.hpp"
28
#include <memory>
29
#include "castor/tape/tapeserver/daemon/Payload.hpp"
30

31

32
33
34
35
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
36
37
38
/**
 * Individual memory block with metadata
 */
39
class MemBlock {
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  
  struct AlterationContext{
    //provide an enumation of type, thus we can 
    //overload the constructor on those types
    struct Failed_t{};
    static Failed_t Failed ;
    
    struct Cancelled_t{}; 
    static Cancelled_t Cancelled;
    
    /** Flag indicating to the receiver that the file read failed */
    bool m_failed;
    
    /** Flag indicating that the transfer was cancelled, usually due to a 
     previous failure. */
    bool m_cancelled;
    
    /**
     * in case of error, the error message 
     */
    std::string m_errorMsg;
    
62
63
64
    /**
     * in case of error, the error message 
     */
65
66
67
68
69
70
71
72
73
74
    int m_errorCode;
    
    AlterationContext(const std::string& msg,int errorCode,Failed_t):
    m_failed(true),m_cancelled(false),m_errorMsg(msg),m_errorCode(errorCode){}
    
    AlterationContext(Cancelled_t):
    m_failed(false),m_cancelled(true),m_errorMsg(""),m_errorCode(0){}
  };
  
  std::auto_ptr<AlterationContext> m_context;
75
public:
76
77
78
79
80
  /**
   * COnstrucor 
   * @param id the block ID for its whole life
   * @param capacity the capacity (in byte) of the embed payload 
   */
81
  MemBlock(const int id, const size_t capacity) : 
82
  m_memoryBlockId(id),m_payload(capacity){
83
84
85
    reset();
  }
  
86
87
88
89
90
  /**
   * Get the error message from the context, 
   * Throw an exception if there is no context
   * @return 
   */
91
92
93
94
95
96
97
98
  std::string errorMsg() const {
    if(m_context.get()) {
      return m_context->m_errorMsg;
    }

    throw castor::exception::Exception("Error Context is not set ="
            " no error message to give");
  }
99

100
101
102
103
104
  /**
   * Get the error code from the context, 
   * Throw an exception if there is no context
   * @return 
   */
105
106
  /* Out of the process, waiting to check if it is a good idea or not 
   * send the error code
107
108
109
110
111
112
113
  int errorCode() const {
    if(m_context.get()) {
      return m_context->m_errorCode;
    }

    throw castor::exception::Exception("Error Context is not set ="
            " no error code to give");
114
  } */
115
116
117
118
119
  
  /**
   * Return true if the block has been marked as failed 
   * @return 
   */
120
121
122
123
  bool isFailed() const {
    return m_context.get() && m_context->m_failed;
  }
  
124
125
126
127
  /**
   * Return true if the block has been marked as canceled 
   * @return 
   */
128
129
130
131
  bool isCanceled() const {
    return m_context.get() && m_context->m_cancelled;
  }
    
132
133
134
135
136
  /**
   * Mark this block as failed ie 
   * m_failed is true, m_fileBlock and m_tapeFileBlock are set at -1
   * Other members do not change
   */
137
138
  void markAsFailed(const std::string msg,int errCode){
    m_context.reset(new AlterationContext(msg,errCode,AlterationContext::Failed));
139
140
141
    m_fileBlock = -1;
    m_tapeFileBlock = -1;
  }
142
  /**
143
   * Mark the block as canceled: this indicates the writer thread that
144
145
146
147
148
149
   * the read was skipped due to previous, unrelated errors, and that this
   * file will not be processed at all (and hence should not be reported about).
   * This is mainly used for the tape read case, when positioning is confused
   * (when positioning by fSeq, there's nothing we can do).
   */
  void markAsCancelled(){
150
    m_context.reset(new AlterationContext(AlterationContext::Cancelled));
151
152
153
    m_fileBlock = -1;
    m_tapeFileBlock = -1;
  }
154
  /**
155
156
   * Reset all the members.
   * Numerical ones are set at -1.and m_failed to false.
157
   */
158
  void reset() throw() {
159
160
161
162
    m_fileid = -1;
    m_fileBlock = -1;
    m_fSeq = -1;
    m_tapeFileBlock = -1;
163
    m_payload.reset();
164
165
166
167
    
    //delete the previous m_context (if allocated) 
    //and set the new one to NULL
    m_context.reset();
168
  }
169
  /** Unique memory block id */
170
  const int m_memoryBlockId;
171
  
172
  /** handle to the raw data to be migrated/recalled */
173
  Payload m_payload;
174
  
175
176
  /** CASTOR NsFileId file concerned */
  uint64_t m_fileid;
177

178
  /** number of the memory-chunk of the current file we are manipulating */
179
  int m_fileBlock;
180
  
181
182
  /** order of file on the tape */
  uint32_t m_fSeq;
183
  
184
  /** Sequence number of the first tape block file in this memory block */
185
  int m_tapeFileBlock;
186
  
187
188
189
  /** Size of the tape blocks, allowing sanity checks on the disk write side in recalls */
  int m_tapeBlockSize;
  
190
};
191
192
193
194
195

}
}
}
}
196