libfoedus-core
FOEDUS Core Library
partitioner.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
19 
20 #include <glog/logging.h>
21 
22 #include <ostream>
23 
24 #include "foedus/engine.hpp"
34 
35 namespace foedus {
36 namespace storage {
37 
39  ASSERT_ND(id > 0);
42  return anchors->partitioner_metadata_ + id;
43 }
44 
48  return anchors->partitioner_metadata_;
49 }
50 
52  ASSERT_ND(data_size_ > 0);
54  <= engine->get_options().storage_.partitioner_data_memory_mb_ * (1ULL << 20));
57  char* buffer = reinterpret_cast<char*>(anchors->partitioner_data_);
58  return buffer + data_offset_;
59 }
60 
62  Engine* engine,
63  soc::SharedMutexScope* locked,
64  uint32_t data_size) {
65  ASSERT_ND(!valid_);
66  ASSERT_ND(data_size > 0);
67  ASSERT_ND(data_size_ == 0);
68  ASSERT_ND(data_offset_ == 0);
69  ASSERT_ND(locked->get_mutex() == &mutex_);
70  ASSERT_ND(locked->is_locked_by_me());
71  PartitionerMetadata* index0 = get_index0_metadata(engine);
72  ASSERT_ND(index0->data_offset_ <= index0->data_size_);
73  soc::SharedMutexScope index0_scope(&index0->mutex_);
74  if (index0->data_offset_ + data_size > index0->data_size_) {
76  }
77  data_offset_ = index0->data_offset_;
78  data_size_ = data_size;
79  index0->data_offset_ += data_size;
80  return kErrorCodeOk;
81 }
82 
83 
85  : Attachable< PartitionerMetadata >(engine, PartitionerMetadata::get_metadata(engine, id)) {
86  id_ = id;
87  type_ = engine->get_storage_manager()->get_storage(id)->meta_.type_;
88  ASSERT_ND(type_ != kInvalidStorage);
89 }
90 
92 bool Partitioner::is_valid() const { return control_block_->valid_; }
93 
95  switch (type_) {
100  default:
101  LOG(FATAL) << "Unsupported storage type:" << type_;
102  return false;
103  }
104 }
105 
107  switch (type_) {
108  case kArrayStorage: return array::ArrayPartitioner(this).design_partition(args);
109  case kHashStorage: return hash::HashPartitioner(this).design_partition(args);
112  default:
113  LOG(FATAL) << "Unsupported storage type:" << type_;
114  return kRetOk;
115  }
116 }
117 
119  switch (type_) {
120  case kArrayStorage: return array::ArrayPartitioner(this).partition_batch(args);
121  case kHashStorage: return hash::HashPartitioner(this).partition_batch(args);
124  default:
125  LOG(FATAL) << "Unsupported storage type:" << type_;
126  }
127 }
128 
130  switch (type_) {
131  case kArrayStorage: return array::ArrayPartitioner(this).sort_batch(args);
132  case kHashStorage: return hash::HashPartitioner(this).sort_batch(args);
135  default:
136  LOG(FATAL) << "Unsupported storage type:" << type_;
137  }
138 }
139 
140 std::ostream& operator<<(std::ostream& o, const Partitioner& v) {
141  o << "<Partitioner>"
142  << "<id>" << v.id_ << "</id>"
143  << "<type>" << to_storage_type_name(v.type_) << "</type>"
144  << *v.control_block_;
145  o << "</Partitioner>";
146  return o;
147 }
148 std::ostream& operator<<(std::ostream& o, const PartitionerMetadata& v) {
149  o << "<PartitionerMetadata>"
150  << "<valid>" << v.valid_ << "</valid>"
151  << "<data_offset_>" << assorted::Hex(v.data_offset_) << "</data_offset_>"
152  << "<data_size_>" << assorted::Hex(v.data_size_) << "</data_size_>"
153  << "</PartitionerMetadata>";
154  return o;
155 }
156 
157 } // namespace storage
158 } // namespace foedus
bool valid_
Whether this partitioner information (metadata+data) has been constructed.
Metadata meta_
common part of the metadata.
Definition: storage.hpp:84
void sort_batch(const SortBatchArguments &args)
Called from log reducer to sort log entries by keys.
storage::StorageManager * get_storage_manager() const
See Storage Manager.
Definition: engine.cpp:60
uint32_t StorageId
Unique ID for storage.
Definition: storage_id.hpp:55
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
void * partitioner_data_
Data block to place detailed information of partitioners.
void partition_batch(const Partitioner::PartitionBatchArguments &args) const
GlobalMemoryAnchors * get_global_memory_anchors()
Partitioner for a hash storage.
Brings error stacktrace information as return value of functions.
Definition: error_stack.hpp:81
0x0825 : "STORAGE: Memory for Partitioners ran out during snapshot. Increase StorageOptions::partitio...
Definition: error_code.hpp:188
0 indicates invalid type.
Definition: storage_id.hpp:124
Partitioner for an array storage.
const EngineOptions & get_options() const
Definition: engine.cpp:39
SharedMutex * get_mutex() const
ErrorStack design_partition(const Partitioner::DesignPartitionArguments &args)
void sort_batch(const Partitioner::SortBatchArguments &args) const
uint32_t partitioner_data_memory_mb_
Size in MB of a shared memory buffer allocated for all partitioners during log gleaning.
0 means no-error.
Definition: error_code.hpp:87
Partitioner(Engine *engine, StorageId id)
Instantiate an instance for the given storage.
Definition: partitioner.cpp:84
storage::PartitionerMetadata * partitioner_metadata_
Tiny metadata memory for partitioners.
PartitionerMetadata * control_block_
The shared data on shared memory that has been initialized in some SOC or master engine.
Definition: attachable.hpp:111
static PartitionerMetadata * get_index0_metadata(Engine *engine)
Returns the special index-0 entry that manages data block allocation for partitioners.
Definition: partitioner.cpp:45
storage::StorageOptions storage_
uint32_t data_size_
The size of the partitioner data.
void sort_batch(const Partitioner::SortBatchArguments &args) const
void sort_batch(const Partitioner::SortBatchArguments &args) const
Database engine object that holds all resources and provides APIs.
Definition: engine.hpp:109
ErrorCode allocate_data(Engine *engine, soc::SharedMutexScope *locked, uint32_t data_size)
Allocates a patitioner data in shared memory of the given size.
Definition: partitioner.cpp:61
Auto-lock scope object for SharedMutex.
const char * to_storage_type_name(StorageType type)
Gives a string representation of StorageType.
Definition: storage_id.hpp:139
void sort_batch(const Partitioner::SortBatchArguments &args) const
soc::SharedMutex mutex_
Serialize concurrent initialization of this partitioner.
Attachable Resources on Shared Memory.
Definition: attachable.hpp:58
uint32_t data_offset_
Relative offset from the beginning of partitioner data block that points to variable-sized partitione...
Just a set of pointers within global_memory_ for ease of use.
StorageType type_
type of the storage.
Definition: metadata.hpp:105
bool is_partitionable()
returns if this storage is partitionable.
Definition: partitioner.cpp:94
Tiny metadata of partitioner for every storage used while log gleaning.
void * locate_data(Engine *engine)
Returns the partitioner data pointed from this metadata.
Definition: partitioner.cpp:51
const ErrorStack kRetOk
Normal return value for no-error case.
void partition_batch(const PartitionBatchArguments &args)
Identifies the partition of each log record in a batched fashion.
soc::SocManager * get_soc_manager() const
See SOC and IPC.
Definition: engine.cpp:59
Partitioning and sorting logic for one storage.
Definition: partitioner.hpp:70
Convenient way of writing hex integers to stream.
bool is_valid() const
whether this object is ready for partitioning.
Definition: partitioner.cpp:92
void partition_batch(const Partitioner::PartitionBatchArguments &args) const
std::ostream & operator<<(std::ostream &o, const Composer &v)
Definition: composer.cpp:39
ErrorStack design_partition(const Partitioner::DesignPartitionArguments &args)
ErrorStack design_partition(const DesignPartitionArguments &args)
Determines partitioning scheme for this storage.
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
StorageControlBlock * get_storage(StorageId id)
Returns the storage of given ID.
void partition_batch(const Partitioner::PartitionBatchArguments &args) const
ErrorStack design_partition(const Partitioner::DesignPartitionArguments &args)
ErrorStack design_partition(const Partitioner::DesignPartitionArguments &args)
ErrorCode
Enum of error codes defined in error_code.xmacro.
Definition: error_code.hpp:85
void partition_batch(const Partitioner::PartitionBatchArguments &args) const
const PartitionerMetadata & get_metadata() const
Returns tiny metadata of the partitioner in shared memory.
Definition: partitioner.cpp:91
static PartitionerMetadata * get_metadata(Engine *engine, StorageId id)
Returns the shared memory for the given storage ID.
Definition: partitioner.cpp:38
SharedMemoryRepo * get_shared_memory_repo()
Returns the shared memories maintained across SOCs.
Definition: soc_manager.cpp:38