• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

HotelsDotCom / waggle-dance / #445

24 Jul 2025 02:27PM UTC coverage: 69.257% (-7.1%) from 76.395%
#445

push

patduin
Merge branch 'hive-3.x' of github.com:ExpediaGroup/waggle-dance into hive-3.x

2620 of 3783 relevant lines covered (69.26%)

0.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.92
/waggle-dance-core/src/main/java/com/hotels/bdp/waggledance/mapping/model/MetaStoreMappingImpl.java
1
/**
2
 * Copyright (C) 2016-2025 Expedia, Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.hotels.bdp.waggledance.mapping.model;
17

18
import java.io.IOException;
19
import java.util.Collections;
20
import java.util.List;
21
import java.util.Locale;
22
import java.util.concurrent.CompletableFuture;
23
import java.util.concurrent.ExecutionException;
24
import java.util.concurrent.ExecutorService;
25
import java.util.concurrent.Executors;
26
import java.util.concurrent.Future;
27
import java.util.concurrent.TimeUnit;
28
import java.util.concurrent.TimeoutException;
29

30
import org.apache.hadoop.hive.metastore.MetaStoreFilterHook;
31
import org.apache.hadoop.hive.metastore.api.AlreadyExistsException;
32
import org.apache.hadoop.hive.metastore.api.Database;
33
import org.apache.hadoop.hive.metastore.api.InvalidObjectException;
34
import org.apache.hadoop.hive.metastore.api.MetaException;
35
import org.apache.hadoop.hive.metastore.api.ThriftHiveMetastore;
36
import org.apache.thrift.TException;
37
import org.slf4j.Logger;
38
import org.slf4j.LoggerFactory;
39

40
import com.hotels.bdp.waggledance.api.model.ConnectionType;
41
import com.hotels.bdp.waggledance.client.CloseableThriftHiveMetastoreIface;
42
import com.hotels.bdp.waggledance.server.security.AccessControlHandler;
43
import com.hotels.bdp.waggledance.server.security.NotAllowedException;
44

45

46
class MetaStoreMappingImpl implements MetaStoreMapping {
47

48
  private final static Logger log = LoggerFactory.getLogger(MetaStoreMappingImpl.class);
1✔
49

50
  // MilliSeconds
51
  static final long DEFAULT_AVAILABILITY_TIMEOUT = 2000;
52

53
  private final String databasePrefix;
54
  private final String name;
55
  private final CloseableThriftHiveMetastoreIface client;
56
  private final AccessControlHandler accessControlHandler;
57
  private final ConnectionType connectionType;
58
  private final long latency;
59
  private final MetaStoreFilterHook metastoreFilter;
60
  private final ExecutorService executor = Executors.newSingleThreadExecutor();
1✔
61

62
  MetaStoreMappingImpl(
63
      String databasePrefix,
64
      String name,
65
      CloseableThriftHiveMetastoreIface client,
66
      AccessControlHandler accessControlHandler,
67
      ConnectionType connectionType,
68
      long latency,
69
      MetaStoreFilterHook metastoreFilter) {
1✔
70
    this.databasePrefix = databasePrefix;
1✔
71
    this.name = name;
1✔
72
    this.client = client;
1✔
73
    this.accessControlHandler = accessControlHandler;
1✔
74
    this.connectionType = connectionType;
1✔
75
    this.latency = latency;
1✔
76
    this.metastoreFilter = metastoreFilter;
1✔
77
  }
1✔
78

79
  @Override
80
  public String transformOutboundDatabaseName(String databaseName) {
81
    return databaseName.toLowerCase(Locale.ROOT);
1✔
82
  }
83

84
  @Override
85
  public List<String> transformOutboundDatabaseNameMultiple(String databaseName) {
86
    return Collections.singletonList(transformInboundDatabaseName(databaseName));
1✔
87
  }
88

89
  @Override
90
  public ThriftHiveMetastore.Iface getClient() {
91
    return client;
1✔
92
  }
93

94
  @Override
95
  public MetaStoreFilterHook getMetastoreFilter() {
96
    return metastoreFilter;
1✔
97
  }
98

99
  @Override
100
  public Database transformOutboundDatabase(Database database) {
101
    database.setName(transformOutboundDatabaseName(database.getName()));
1✔
102
    return database;
1✔
103
  }
104

105
  @Override
106
  public String transformInboundDatabaseName(String databaseName) {
107
    return databaseName.toLowerCase(Locale.ROOT);
1✔
108
  }
109

110
  @Override
111
  public String getDatabasePrefix() {
112
    return databasePrefix;
1✔
113
  }
114

115
  @Override
116
  public void close() throws IOException {
117
    client.close();
1✔
118
    executor.shutdownNow();
1✔
119
  }
1✔
120

121
  /**
122
   * This is potentially slow so a best effort is made and false is returned after a timeout.
123
   */
124
  @Override
125
  public boolean isAvailable() {
126
    Future<Boolean> future = CompletableFuture.supplyAsync(() -> {
1✔
127
      try {
128
        boolean isOpen = client.isOpen();
1✔
129
        if (isOpen && connectionType == ConnectionType.TUNNELED) {
1✔
130
          client.getStatus();
1✔
131
        }
132
        return isOpen;
1✔
133
      } catch (Exception e) {
1✔
134
        log.error("Metastore Mapping {} unavailable", name, e);
1✔
135
        return false;
1✔
136
      }
137
    }, executor);
138
    long timeout = DEFAULT_AVAILABILITY_TIMEOUT + getLatency();
1✔
139
    try {
140
      return future.get(timeout, TimeUnit.MILLISECONDS);
1✔
141
    } catch (TimeoutException e) {
1✔
142
      log.info("Took too long (>" + timeout + "ms) to check availability of '" + name + "', assuming unavailable");
1✔
143
      future.cancel(true);
1✔
144
    } catch (InterruptedException | ExecutionException e) {
×
145
      log.error("Error while checking availability '" + name + "', assuming unavailable");
×
146
    }
1✔
147
    return false;
1✔
148
  }
149

150
  @Override
151
  public MetaStoreMapping checkWritePermissions(String databaseName) {
152
    if (!accessControlHandler.hasWritePermission(databaseName)) {
1✔
153
      throw new NotAllowedException(
1✔
154
          "You cannot perform this operation on the virtual database '" + databaseName + "'.");
155
    }
156
    return this;
1✔
157
  }
158

159
  @Override
160
  public void createDatabase(Database database)
161
    throws AlreadyExistsException, InvalidObjectException, MetaException, TException {
162
    if (accessControlHandler.hasCreatePermission()) {
1✔
163
      getClient().create_database(database);
1✔
164
      accessControlHandler.databaseCreatedNotification(database.getName());
1✔
165
    } else {
166
      throw new NotAllowedException("You cannot create the database '" + database.getName() + "'.");
1✔
167
    }
168
  }
1✔
169

170
  @Override
171
  public String getMetastoreMappingName() {
172
    return name;
1✔
173
  }
174

175
  @Override
176
  public long getLatency() {
177
    return latency;
1✔
178
  }
179

180
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc