0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
OperationSystemUpgrade.cc
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 #include "Common/Compat.h"
23 #include "Common/Error.h"
24 #include "Common/FailureInducer.h"
25 #include "Common/ScopeGuard.h"
26 #include "Common/System.h"
27 
28 #include "OperationSystemUpgrade.h"
29 #include "Utility.h"
30 
31 using namespace Hypertable;
32 using namespace Hyperspace;
33 
35  : OperationEphemeral(context, MetaLog::EntityType::OPERATION_SYSTEM_UPGRADE) {
36 }
37 
39  const MetaLog::EntityHeader &header_)
40  : OperationEphemeral(context, header_) {
41 }
42 
43 
44 bool OperationSystemUpgrade::update_schema(const String &name, const String &schema_file) {
45  uint64_t handle = 0;
46  String table_id;
47  SchemaPtr old_schema, new_schema;
48  DynamicBuffer value_buf;
49 
51 
52  if (!m_context->namemap->name_to_id(name, table_id))
53  return false;
54 
55  String tablefile = m_context->toplevel_dir + "/tables/" + table_id;
56 
57  handle = m_context->hyperspace->open(tablefile, OPEN_FLAG_READ|OPEN_FLAG_WRITE|OPEN_FLAG_LOCK_EXCLUSIVE);
58  if (!m_context->hyperspace->attr_exists(handle, "schema"))
59  return false;
60 
61  m_context->hyperspace->attr_get(handle, "schema", value_buf);
62  old_schema.reset( Schema::new_instance((const char *)value_buf.base) );
63 
64  String schema_str = FileUtils::file_to_string( System::install_dir + schema_file );
65  new_schema.reset( Schema::new_instance(schema_str) );
66 
67  if (old_schema->get_generation() < new_schema->get_generation()) {
68  m_context->hyperspace->attr_set(handle, "schema", schema_str.c_str(),
69  schema_str.length());
70  HT_INFOF("Upgraded schema for '%s' from generation %lld to %lld", name.c_str(),
71  (Lld)old_schema->get_generation(), (Lld)new_schema->get_generation());
72  return true;
73  }
74  return false;
75 }
76 
77 
79  int32_t state = get_state();
80 
81  HT_INFOF("Entering SystemUpgrade-%lld", (Lld)header.id);
82 
83  switch (state) {
84 
86  if (update_schema("/sys/METADATA", "/conf/METADATA.xml"))
87  m_context->metadata_table = m_context->new_table(TableIdentifier::METADATA_NAME);
88 
89  if (update_schema("/sys/RS_METRICS", "/conf/RS_METRICS.xml"))
90  m_context->rs_metrics_table = m_context->new_table("sys/RS_METRICS");
91 
92  complete_ok();
93  break;
94 
95  default:
96  HT_FATALF("Unrecognized state %d", state);
97  }
98 
99  HT_INFOF("Leaving SystemUpgrade-%lld", (Lld)header.id);
100 }
101 
103  return "OperationSystemUpgrade";
104 }
105 
107  return String("SystemUpgrade");
108 }
109 
Retrieves system information (hardware, installation directory, etc)
The FailureInducer simulates errors.
ContextPtr m_context
Pointer to Master context.
Definition: Operation.h:553
bool update_schema(const String &name, const String &schema_file)
Abstract base class for ephemeral operations.
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
int64_t id
Unique ID of entity.
static const char * METADATA_NAME
EntityHeader header
Entity header
#define HT_ON_SCOPE_EXIT(...)
Definition: ScopeGuard.h:301
A dynamic, resizable and reference counted memory buffer.
Definition: DynamicBuffer.h:42
std::shared_ptr< Context > ContextPtr
Smart pointer to Context.
Definition: Context.h:265
Hyperspace definitions
virtual const String label()
Human readable label for operation.
Open file for writing.
Definition: Session.h:73
void close_handle_ptr(SessionPtr hyperspace, uint64_t *handlep)
Definition: Session.cc:1400
Compatibility Macros for C/C++.
Hypertable definitions
static Schema * new_instance(const std::string &buf)
Creates schema object from XML schema string.
Definition: Schema.cc:202
#define HT_FATALF(msg,...)
Definition: Logger.h:343
long long int Lld
Shortcut for printf formats.
Definition: String.h:53
Declarations for general-purpose utility functions.
#define HT_INFOF(msg,...)
Definition: Logger.h:272
atomically open and lock file exclusive, fail if can't
Definition: Session.h:85
uint8_t * base
Pointer to the allocated memory buffer.
static String install_dir
The installation directory.
Definition: System.h:114
std::shared_ptr< Schema > SchemaPtr
Smart pointer to Schema.
Definition: Schema.h:465
void complete_ok(std::vector< MetaLog::EntityPtr > &additional)
Definition: Operation.cc:436
Open file for reading.
Definition: Session.h:71
Error codes, Exception handling, error logging.
static String file_to_string(const String &fname)
Reads a full file into a String.
Definition: FileUtils.cc:333
virtual const String name()
Name of operation used for exclusivity.
virtual void execute()
Executes (carries out) the operation.
Executes user-defined functions when leaving the current scope.