VolumeDataRequestProcessor.cpp 6.47 KB
Newer Older
Jørgen Lind's avatar
Jørgen Lind committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/****************************************************************************
** Copyright 2019 The Open Group
** Copyright 2019 Bluware, Inc.
**
** 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.
****************************************************************************/

#include "VolumeDataRequestProcessor.h"

#include "VolumeDataChunk.h"
#include "VolumeDataChannelMapping.h"
22
#include "VolumeDataLayoutImpl.h"
Jørgen Lind's avatar
Jørgen Lind committed
23
#include "VolumeDataPageImpl.h"
Jørgen Lind's avatar
Jørgen Lind committed
24
#include "VDS.h"
Jørgen Lind's avatar
Jørgen Lind committed
25

26
#include <cstdint>
Jørgen Lind's avatar
Jørgen Lind committed
27
28
#include <algorithm>
#include <atomic>
Jørgen Lind's avatar
Jørgen Lind committed
29
30
31
32
#include <thread>
#include <chrono>

#include <fmt/format.h>
Jørgen Lind's avatar
Jørgen Lind committed
33
34
35
36
37
38

namespace OpenVDS
{

VolumeDataRequestProcessor::VolumeDataRequestProcessor(VolumeDataAccessManagerImpl& manager)
  : m_manager(manager)
Jørgen Lind's avatar
Jørgen Lind committed
39
  , m_threadPool(std::thread::hardware_concurrency())
Jørgen Lind's avatar
Jørgen Lind committed
40
41
{}

42
43
44
45
46
47
48
49
VolumeDataRequestProcessor::~VolumeDataRequestProcessor()
{
  for (auto &pair : m_pageAccessors)
  {
    m_manager.DestroyVolumeDataPageAccessor(pair.second);
  }
}

50
static int64_t GenJobId()
Jørgen Lind's avatar
Jørgen Lind committed
51
{
52
  static std::atomic< std::int64_t > id(0);
Jørgen Lind's avatar
Jørgen Lind committed
53
54
55
  return ++id;
}

Jørgen Lind's avatar
Jørgen Lind committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
struct MarkJobAsDoneOnExit
{
  MarkJobAsDoneOnExit(Job *job)
    : job(job)
  {}
  ~MarkJobAsDoneOnExit()
  {
    int completed = job->completed.fetch_add(1);
    if (completed == job->pages.size() - 1)
    {
      job->doneNotify.notify_all();
    }
  }
  Job *job;
};

72
static Error ProcessPageInJob(Job *job, size_t pageIndex, VolumeDataPageAccessorImpl *pageAccessor, std::function<bool(VolumeDataPageImpl *page, const VolumeDataChunk &chunk, Error &error)> processor)
Jørgen Lind's avatar
Jørgen Lind committed
73
74
75
76
77
78
{
  MarkJobAsDoneOnExit jobDone(job);
  Error error;
  JobPage& jobPage = job->pages[pageIndex];
  if (jobPage.needToReadPage)
  {
79
    if (!pageAccessor->ReadPreparedPaged(jobPage.page))
Jørgen Lind's avatar
Jørgen Lind committed
80
    {
81
      error.Code = -1;
82
      error.String = fmt::format("Failed to read page {}.", jobPage.page->GetChunkIndex());
Jørgen Lind's avatar
Jørgen Lind committed
83
84
85
86
87
      return error;
    }
  }
  if (job->cancelled)
  {
88
89
    error.Code = -4;
    error.String = fmt::format("Request: {} has been cancelled.", job->jobId);
Jørgen Lind's avatar
Jørgen Lind committed
90
91
92
93
    return error;
  }

  processor(jobPage.page, jobPage.chunk, error);
94
  jobPage.page->UnPin();
Jørgen Lind's avatar
Jørgen Lind committed
95
96
97
98

  return error;
}

99
int64_t VolumeDataRequestProcessor::AddJob(const std::vector<VolumeDataChunk>& chunks, std::function<bool(VolumeDataPageImpl * page, const VolumeDataChunk &volumeDataChunk, Error & error)> processor)
Jørgen Lind's avatar
Jørgen Lind committed
100
{
101
102
103
104
105
  auto layer = chunks.front().Layer;
  DimensionsND dimensions = DimensionGroupUtil::GetDimensionsNDFromDimensionGroup(layer->GetPrimaryChannelLayer().GetChunkDimensionGroup());
  int channel = layer->GetChannelIndex();
  int lod = layer->GetLOD();
  auto layout = layer->GetLayout();
Jørgen Lind's avatar
Jørgen Lind committed
106

107
108
  const int maxPages = std::max(8, (int)chunks.size());

Jørgen Lind's avatar
Jørgen Lind committed
109
  std::unique_lock<std::mutex> lock(m_mutex);
Jørgen Lind's avatar
Jørgen Lind committed
110
  PageAccessorKey key = { dimensions, lod, channel };
Jørgen Lind's avatar
Jørgen Lind committed
111
112
  auto page_accessor_it = m_pageAccessors.find(key);
  if (page_accessor_it == m_pageAccessors.end())
Jørgen Lind's avatar
Jørgen Lind committed
113
  {
114
    auto pa = static_cast<VolumeDataPageAccessorImpl *>(m_manager.CreateVolumeDataPageAccessor(layout, dimensions, lod, channel, maxPages, OpenVDS::VolumeDataAccessManager::AccessMode_ReadOnly));
Jørgen Lind's avatar
Jørgen Lind committed
115
    auto insert_result = m_pageAccessors.insert({key, pa});
Jørgen Lind's avatar
Jørgen Lind committed
116
117
118
    assert(insert_result.second);
    page_accessor_it = insert_result.first;
  }
119

120
  m_jobs.emplace_back(new Job(GenJobId(), m_jobNotification));
Jørgen Lind's avatar
Jørgen Lind committed
121
122
  auto &job = m_jobs.back();
  job->pages.reserve(chunks.size());
123

Jørgen Lind's avatar
Jørgen Lind committed
124
  VolumeDataPageAccessorImpl *pageAccessor = page_accessor_it->second;
125
  if(pageAccessor->GetMaxPages() < maxPages)
126
  {
127
    pageAccessor->SetMaxPages(maxPages);
128
129
  }

Jørgen Lind's avatar
Jørgen Lind committed
130
131
  for (const auto &c : chunks)
  {
Jørgen Lind's avatar
Jørgen Lind committed
132
133
    job->pages.emplace_back();
    JobPage &jobPage = job->pages.back();
134
    jobPage.page = static_cast<VolumeDataPageImpl *>(pageAccessor->PrepareReadPage(c.Index, &jobPage.needToReadPage));
135
    assert(jobPage.page && "Need to add error handling here when the page cannot be read");
Jørgen Lind's avatar
Jørgen Lind committed
136
137
    jobPage.chunk = c;
    size_t index = job->pages.size() - 1;
138
    auto job_ptr = job.get();
139
    job->future.push_back(m_threadPool.Enqueue([job_ptr, index, pageAccessor, processor] { return ProcessPageInJob(job_ptr, index, pageAccessor, processor); }));
Jørgen Lind's avatar
Jørgen Lind committed
140
  }
Jørgen Lind's avatar
Jørgen Lind committed
141
  return job->jobId;
Jørgen Lind's avatar
Jørgen Lind committed
142
143
}

144
bool  VolumeDataRequestProcessor::IsCompleted(int64_t jobID)
Jørgen Lind's avatar
Jørgen Lind committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
{
  std::unique_lock<std::mutex> lock(m_mutex);
  auto job_it = std::find_if(m_jobs.begin(), m_jobs.end(), [jobID](const std::unique_ptr<Job> &job) { return job->jobId == jobID; });
  if (job_it == m_jobs.end())
    return false;

  if (job_it->get()->completed == job_it->get()->pages.size() && !job_it->get()->cancelled)
  {
    m_jobs.erase(job_it);
    return true;
  }
  return false;
}
  
159
bool VolumeDataRequestProcessor::IsCanceled(int64_t jobID)
Jørgen Lind's avatar
Jørgen Lind committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
{
  std::unique_lock<std::mutex> lock(m_mutex);
  auto job_it = std::find_if(m_jobs.begin(), m_jobs.end(), [jobID](const std::unique_ptr<Job> &job) { return job->jobId == jobID; });
  if (job_it == m_jobs.end())
    return false;
  
  if (job_it->get()->completed == job_it->get()->pages.size() && job_it->get()->cancelled)
  {
    m_jobs.erase(job_it);
    return true;
  }
  return false;
}
  
174
bool VolumeDataRequestProcessor::WaitForCompletion(int64_t jobID, int millisecondsBeforeTimeout)
Jørgen Lind's avatar
Jørgen Lind committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
{
  std::unique_lock<std::mutex> lock(m_mutex);
  auto job_it = std::find_if(m_jobs.begin(), m_jobs.end(), [jobID](const std::unique_ptr<Job> &job) { return job->jobId == jobID; });
  if (job_it == m_jobs.end())
    return false;

  Job *job = job_it->get();
  if (millisecondsBeforeTimeout > 0)
  {
    std::chrono::milliseconds toWait(millisecondsBeforeTimeout);
    job_it->get()->doneNotify.wait_for(lock, toWait, [job]{ return job->completed == job->pages.size();});
  } else
  {
    job_it->get()->doneNotify.wait(lock, [job]{ return job->completed == job->pages.size();});
  }

  if (job->completed == job->pages.size() && !job->cancelled)
  {
    m_jobs.erase(job_it);
    return true;
  }
  return false;
}

199
void VolumeDataRequestProcessor::Cancel(int64_t jobID)
Jørgen Lind's avatar
Jørgen Lind committed
200
201
202
203
204
205
206
{
  std::unique_lock<std::mutex> lock(m_mutex);
  auto job_it = std::find_if(m_jobs.begin(), m_jobs.end(), [jobID](std::unique_ptr<Job> &job) { return job->jobId == jobID; });
  if (job_it == m_jobs.end())
    return;
  job_it->get()->cancelled = true;
}
Jørgen Lind's avatar
Jørgen Lind committed
207
208

}