Signed-off-by: Sugu Sougoumarane <ssougou@gmail.com>
This commit is contained in:
Sugu Sougoumarane 2020-04-05 14:07:16 -07:00
Родитель ee017433c9
Коммит 0fe7fdc6c2
4 изменённых файлов: 1 добавлений и 274 удалений

Просмотреть файл

@ -1,183 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2019 The Vitess Authors.
#
# 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.
"""This module allows you to bring up and tear down keyspaces."""
import cgi
import decimal
import json
import subprocess
import threading
import time
from vtdb import keyrange
from vtdb import vtgate_client
# TODO(sougou): remove this import once the deps are fixed
import google.protobuf
from vtdb import grpc_vtgate_client # pylint: disable=unused-import
def exec_query(conn, title, query, response, keyspace=None, kr=None): # pylint: disable=missing-docstring
if kr:
# v2 cursor to address individual shards directly, for debug display
cursor = conn.cursor(
tablet_type="master", keyspace=keyspace,
keyranges=[keyrange.KeyRange(kr)])
else:
# v3 cursor is automated
cursor = conn.cursor(
tablet_type="master", keyspace=keyspace, writable=True)
try:
if not query or query == "undefined":
return
if query.startswith("select"):
cursor.execute(query, {})
else:
cursor.begin()
cursor.execute(query, {})
cursor.commit()
response[title] = {
"title": title,
"description": cursor.description,
"rowcount": cursor.rowcount,
"lastrowid": cursor.lastrowid,
"results": cursor.results,
}
cursor.close()
except Exception as e: # pylint: disable=broad-except
response[title] = {
"title": title,
"error": str(e),
}
cursor.rollback()
cursor.close()
def capture_log(port, queries): # pylint: disable=missing-docstring
p = subprocess.Popen(
["curl", "-s", "-N", "http://localhost:%d/debug/querylog" % port],
stdout=subprocess.PIPE)
def collect():
for line in iter(p.stdout.readline, ""):
query = line.split("\t")[12].strip('"')
if not query:
continue
queries.append(query)
t = threading.Thread(target=collect)
t.daemon = True
t.start()
return p
def main():
print "Content-Type: application/json\n"
try:
conn = vtgate_client.connect("grpc", "localhost:12346", 10.0)
args = cgi.FieldStorage()
query = args.getvalue("query")
response = {}
try:
queries = []
stats = capture_log(12345, queries)
time.sleep(0.25)
exec_query(conn, "result", query, response)
finally:
stats.terminate()
time.sleep(0.25)
response["queries"] = queries
# user table
exec_query(
conn, "user0",
"select * from user", response, keyspace="user", kr="-80")
exec_query(
conn, "user1",
"select * from user", response, keyspace="user", kr="80-")
# user_extra table
exec_query(
conn, "user_extra0",
"select * from user_extra", response, keyspace="user", kr="-80")
exec_query(
conn, "user_extra1",
"select * from user_extra", response, keyspace="user", kr="80-")
# music table
exec_query(
conn, "music0",
"select * from music", response, keyspace="user", kr="-80")
exec_query(
conn, "music1",
"select * from music", response, keyspace="user", kr="80-")
# music_extra table
exec_query(
conn, "music_extra0",
"select * from music_extra", response, keyspace="user", kr="-80")
exec_query(
conn, "music_extra1",
"select * from music_extra", response, keyspace="user", kr="80-")
# name_info table
exec_query(
conn, "name_info0",
"select * from name_info", response, keyspace="user", kr="-80")
exec_query(
conn, "name_info1",
"select * from name_info", response, keyspace="user", kr="80-")
# music_keyspace_idx table
exec_query(
conn, "music_keyspace_idx0",
"select music_id, hex(keyspace_id) from music_keyspace_idx", response, keyspace="user", kr="-80")
exec_query(
conn, "music_keyspace_idx1",
"select music_id, hex(keyspace_id) from music_keyspace_idx", response, keyspace="user", kr="80-")
# lookup tables
exec_query(
conn, "user_seq", "select * from user_seq", response,
keyspace="lookup", kr="-")
exec_query(
conn, "music_seq", "select * from music_seq", response,
keyspace="lookup", kr="-")
exec_query(
conn, "name_keyspace_idx", "select name, hex(keyspace_id) from name_keyspace_idx", response,
keyspace="lookup", kr="-")
print json.dumps(response, default=decimal_default)
except Exception as e: # pylint: disable=broad-except
print json.dumps({"error": str(e)})
def decimal_default(obj):
"""Provide json-encodable conversion for decimal.Decimal type.
json encoding fails on decimal.Decimal. This
function converts the decimal into a float object
which json knows how to encode.
"""
if isinstance(obj, decimal.Decimal):
return float(obj)
raise TypeError
if __name__ == "__main__":
main()

Просмотреть файл

@ -115,6 +115,6 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.js" type="text/javascript"></script>
<script src="demo.js" type="text/javascript"></script>
<script src="index.js" type="text/javascript"></script>
</body>
</html>

Просмотреть файл

Просмотреть файл

@ -1,90 +0,0 @@
#!/usr/bin/env python
# Copyright 2019 The Vitess Authors.
#
# 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.
"""This is a demo for V3 features.
The script will launch all the processes necessary to bring up
the demo. It will bring up an HTTP server on port 8000 by default,
which you can override. Once done, hitting <Enter> will terminate
all processes. Vitess will always be started on port 12345.
"""
import json
import optparse
import os
import subprocess
import thread
from CGIHTTPServer import CGIHTTPRequestHandler
from BaseHTTPServer import HTTPServer
from google.protobuf import text_format
from vtproto import vttest_pb2
def start_http_server(port):
httpd = HTTPServer(('', port), CGIHTTPRequestHandler)
thread.start_new_thread(httpd.serve_forever, ())
def start_vitess():
"""This is the main start function."""
topology = vttest_pb2.VTTestTopology()
keyspace = topology.keyspaces.add(name='user')
keyspace.shards.add(name='-80')
keyspace.shards.add(name='80-')
keyspace = topology.keyspaces.add(name='lookup')
keyspace.shards.add(name='0')
vtroot = os.environ['VTROOT']
args = [os.path.join(vtroot, 'py/vttest/run_local_database.py'),
'--port', '12345',
'--proto_topo', text_format.MessageToString(topology,
as_one_line=True),
'--schema_dir', os.path.join(vttop, 'examples/demo/schema'),
'--mysql_server_bind_address', '0.0.0.0']
sp = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
# This load will make us wait for vitess to come up.
print json.loads(sp.stdout.readline())
return sp
def stop_vitess(sp):
sp.stdin.write('\n')
sp.wait()
def main():
parser = optparse.OptionParser()
parser.add_option('-p', '--port', default=8000, help='http server port')
(options, unused_args) = parser.parse_args()
sp = start_vitess()
try:
start_http_server(options.port)
raw_input('\n'
'Demo is running at: http://localhost:%d/\n'
'\n'
'Press enter to exit.\n' % options.port)
finally:
stop_vitess(sp)
if __name__ == '__main__':
main()