2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 #ifndef SQUID_IPC_IOFILE_H
10 #define SQUID_IPC_IOFILE_H
12 #include "base/AsyncCall.h"
14 #include "DiskIO/DiskFile.h"
15 #include "DiskIO/IORequestor.h"
16 #include "ipc/forward.h"
17 #include "ipc/mem/Page.h"
18 #include "SquidString.h"
25 class FewToFewBiQueue
;
28 // TODO: expand to all classes
32 /// what kind of I/O the disker needs to do or have done
33 typedef enum { cmdNone
, cmdOpen
, cmdRead
, cmdWrite
} Command
;
37 /// converts DiskIO requests to IPC queue messages
44 unsigned int requestId
; ///< unique for requestor; matches request w/ response
48 Ipc::Mem::PageId page
;
50 IpcIo::Command command
; ///< what disker is supposed to do or did
51 struct timeval start
; ///< when the I/O request was converted to IpcIoMsg
53 int xerrno
; ///< I/O error code or zero
56 class IpcIoPendingRequest
;
58 class IpcIoFile
: public DiskFile
60 CBDATA_CLASS(IpcIoFile
);
63 typedef RefCount
<IpcIoFile
> Pointer
;
65 IpcIoFile(char const *aDb
);
69 virtual void configure(const Config
&cfg
);
70 virtual void open(int flags
, mode_t mode
, RefCount
<IORequestor
> callback
);
71 virtual void create(int flags
, mode_t mode
, RefCount
<IORequestor
> callback
);
72 virtual void read(ReadRequest
*);
73 virtual void write(WriteRequest
*);
75 virtual bool error() const;
76 virtual int getFD() const;
77 virtual bool canRead() const;
78 virtual bool canWrite() const;
79 virtual bool ioInProgress() const;
81 /// handle open response from coordinator
82 static void HandleOpenResponse(const Ipc::StrandSearchResponse
&response
);
84 /// handle queue push notifications from worker or disker
85 static void HandleNotification(const Ipc::TypedMsgHdr
&msg
);
87 DiskFile::Config config
; ///< supported configuration options
90 friend class IpcIoPendingRequest
;
91 void openCompleted(const Ipc::StrandSearchResponse
*const response
);
92 void readCompleted(ReadRequest
*readRequest
, IpcIoMsg
*const response
);
93 void writeCompleted(WriteRequest
*writeRequest
, const IpcIoMsg
*const response
);
97 void trackPendingRequest(const unsigned int id
, IpcIoPendingRequest
*const pending
);
98 void push(IpcIoPendingRequest
*const pending
);
99 IpcIoPendingRequest
*dequeueRequest(const unsigned int requestId
);
101 static void Notify(const int peerId
);
103 static void OpenTimeout(void *const param
);
104 static void CheckTimeouts(void *const param
);
105 void checkTimeouts();
106 void scheduleTimeoutCheck();
108 static void HandleResponses(const char *const when
);
109 void handleResponse(IpcIoMsg
&ipcIo
);
111 static void DiskerHandleMoreRequests(void*);
112 static void DiskerHandleRequests();
113 static void DiskerHandleRequest(const int workerId
, IpcIoMsg
&ipcIo
);
114 static bool WaitBeforePop();
117 const String dbName
; ///< the name of the file we are managing
118 int diskId
; ///< the process ID of the disker we talk to
119 RefCount
<IORequestor
> ioRequestor
;
121 bool error_
; ///< whether we have seen at least one I/O error (XXX)
123 unsigned int lastRequestId
; ///< last requestId used
125 /// maps requestId to the handleResponse callback
126 typedef std::map
<unsigned int, IpcIoPendingRequest
*> RequestMap
;
127 RequestMap requestMap1
; ///< older (or newer) pending requests
128 RequestMap requestMap2
; ///< newer (or older) pending requests
129 RequestMap
*olderRequests
; ///< older requests (map1 or map2)
130 RequestMap
*newerRequests
; ///< newer requests (map2 or map1)
131 bool timeoutCheckScheduled
; ///< we expect a CheckTimeouts() call
133 static const double Timeout
; ///< timeout value in seconds
135 typedef std::list
<Pointer
> IpcIoFileList
;
136 static IpcIoFileList WaitingForOpen
; ///< pending open requests
138 ///< maps diskerId to IpcIoFile, cleared in destructor
139 typedef std::map
<int, IpcIoFile
*> IpcIoFilesMap
;
140 static IpcIoFilesMap IpcIoFiles
;
142 typedef Ipc::FewToFewBiQueue Queue
;
143 static std::unique_ptr
<Queue
> queue
; ///< IPC queue
145 /// whether we are waiting for an event to handle still queued I/O requests
146 static bool DiskerHandleMoreRequestsScheduled
;
149 /// keeps original I/O request parameters while disker is handling the request
150 class IpcIoPendingRequest
153 IpcIoPendingRequest(const IpcIoFile::Pointer
&aFile
);
155 /// called when response is received and, with a nil response, on timeouts
156 void completeIo(IpcIoMsg
*const response
);
159 const IpcIoFile::Pointer file
; ///< the file object waiting for the response
160 ReadRequest
*readRequest
; ///< set if this is a read requests
161 WriteRequest
*writeRequest
; ///< set if this is a write request
164 IpcIoPendingRequest(const IpcIoPendingRequest
&d
); // not implemented
165 IpcIoPendingRequest
&operator =(const IpcIoPendingRequest
&d
); // ditto
168 #endif /* SQUID_IPC_IOFILE_H */