Cassandra cqlsh code analysis

Source: Internet
Author: User
1. cqlsh code process 1) Start entry

Bin/cqlsh

-Main-

-> Main (* read_options (SYS. argv [1:], OS. Environ ))

-> Def main (options, hostname, Port ):

-> Shell. nested loop ()

-> Nested loop (Self): // enters the CMD loop.

 

2) select entry

Bin/cqlsh

A) def do_select (self, parsed ):

Tracing_was_enabled = self. tracing_enabled

Ksname = parsed. get_binding ('ksname ')

Stop_tracing = ksname = 'System _ traces' or (ksname is none and self. current_keyspace = 'System _ traces ')

Self. tracing_enabled = self. tracing_enabled and not stop_tracing

Statement = parsed. extract_orig ()

With_default_limit = parsed. get_binding ('limit') is none

Ifwith_default_limit:

Statement = "% s limit % d;" % (statement [:-1], default_select_limit)

Self. Merge m_statement (statement, with_default_limit = with_default_limit) # enter the Query Process

Self. tracing_enabled = tracing_was_enabled

You can use the tracing switch and run the following command on the shell command line:

B) Self. m_m_statement (statement, decoder = errorhandlingschemadecoder, with_default_limit = with_default_limit );

C) required m_statement_untraced (self, statement, decoder = none, with_default_limit = false)

Parse the code in untraced status;

D) self.cursor.exe cute (statement, decoder = decoder) # Run the code;

-> Self. cursor = self. Conn. cursor ()

-> Self. Conn = cql. Connect (hostname, port, user = username, password = password,

Cql_version = cqlver, transport = transport); # connect to the cluster when cqlsh is enabled

E) Import cql is implemented in the LIB/cql-internal-only-1.4.1.zip module;

F) open connection. py;

-> Def cursor (Self ):

If notself. open_socket:

Raise programmingerror ("connection has been closed .")

Curs = self. cursorclass (Self)

Curs. Compression = self. Compression

Curs. consistency_level = self. consistency_level

Return curs

G) Open cursor. py

-> Def execute (self, cql_query, Params = {}, decoder = none, consistency_level = none ):

# Note that 'decoder' here is actually the decoder class, not

# Instance tobe used for decoding. Bad naming, but it's in use now.

Ifisinstance (cql_query, Unicode ):

Raise valueerror ("cql query must be bytes, not Unicode ")

Self. pre_execution_setup () # execution stage settings

Prepared_q = self. prepare_inline (cql_query, Params)

CL = consistency_level or self. consistency_level

Response = self. GET_RESPONSE (prepared_q, Cl) # obtain the execution result

Returnself. process_execution_results (response, decoder = decoder)

H) The downloading code requires in-depth analysis.

Find the analysis process of. GET_RESPONSE

I) $ grep 'get _ response './*
./Cursor. py: Response = self. GET_RESPONSE (prepared_q, Cl)
./Cursor. py: Response = self. get_response_prepared (prepared_query, Params, Cl)
./Native. py: def GET_RESPONSE (self, query, consistency_level ):
./Native. py: def get_response_prepared (self, prepared_query, Params, consistency_level ):
./Thriteries. py: def GET_RESPONSE (self, cql_query, consistency_level): # after testing, this file implements the previous functions;
./Thriteries. py: def get_response_prepared (self, prepared_query, Params, consistency_level ):

 

J)./thriteries. py: defget_response (self, cql_query, consistency_level ):

Def GET_RESPONSE (self, cql_query, consistency_level ):

Compressed_q, compress = self. compress_query_text (cql_query) // The statement is compressed.

Print "thrift_getresponse"

CL = getattr (consistencylevel, consistency_level)

IfSelf. use_cql3_methods: # Cassandra is cql3.0 by default.

Doquery = self._connection.client.exe cute_cql3_query # obtain the execution Method

Return self. handle_cql_execution_errors (doquery, compressed_q, compress, Cl )//

Else: # view the method Execution Process

Doquery = self._connection.client.exe cute_cql_query

Return self. handle_cql_execution_errors (doquery, compressed_q, compress)

K) def handle_cql_execution_errors (self, executor, * ARGs, ** kwargs ):

Try:

Return Executor (* ARGs, ** kwargs) // call the executor method to execute the result.

Except tinvalidrequestexception, IRE:

Raise cql. programmingerror ("Bad request: % s" % ire. Why)

Effectschemadisagreementexception, SDE:

Raise cql. integrityerror ("schema versions disagree, (try againlater ).")

Exceptunavailableexception:

Raise cql. operationalerror ("unable to complete request: one or"

"More nodes were unavailable .")

Effecttimedoutexception:

Raise cql. operationalerror ("request did not complete within rpc_timeout .")

Except ttapplicationexception, tapp:

 

L) doquery using self._connection.client.exe cute_prepared_cql3_query

[Lib] $ grep-r 'Prepare _ cql3_query './cql-1.4.1 /*
./Cql-1.4.1/cql/thriteries. py: doquery = self. _ connection. Client. prepare_cql3_query
/Cql-1.4.1/cql/Cassandra. py: defprepare_cql3_query (self, query, compression ):
/Cql-1.4.1/cql/Cassandra. py: defprepare_cql3_query (self, query, compression ):
./Cql-1.4.1/cql/Cassandra. py: Self. send_prepare_cql3_query (query, compression)
/Cql-1.4.1/cql/Cassandra. py: returnself. recv_prepare_cql3_query ()
./Cql-1.4.1/cql/Cassandra. py: defsend_prepare_cql3_query (self, query, compression ):
./Cql-1.4.1/cql/Cassandra. py: Self. _ oprot. writemessagebegin ('Prepare _ cql3_query ', tmessagetype. Call, self. _ seqid)
/Cql-1.4.1/cql/Cassandra. py: ARGs = prepare_cql3_query_args ()
/Cql-1.4.1/cql/Cassandra. py: def recv_prepare_cql3_query (self ,):
./Cql-1.4.1/cql/Cassandra. py: Result = prepare_cql3_query_result ()
./Cql-1.4.1/cql/Cassandra. py: raisetapplicationexception (tapplicationexception. missing_result, "prepare_cql3_query failed: Unknown result ");
./Cql-1.4.1/cql/Cassandra. py: Self. _ processmap ["prepare_cql3_query"] = processor. process_prepare_cql3_query
/Cql-1.4.1/cql/Cassandra. py: defprocess_prepare_cql3_query (self, seqid, iprot, oprot ):
/Cql-1.4.1/cql/Cassandra. py: ARGs = prepare_cql3_query_args ()
./Cql-1.4.1/cql/Cassandra. py: Result = prepare_cql3_query_result ()
../Cql-1.4.1/cql/Cassandra. py: result. Success = self. _ handler. prepare_cql3_query (ARGs. query, argS. Compression)
/Cql-1.4.1/cql/Cassandra. py: oprot. writemessagebegin ("prepare_cql3_query", tmessagetype. Reply, seqid)
/Cql-1.4.1/cql/Cassandra. py: classprepare_cql3_query_args:
./Cql-1.4.1/cql/Cassandra. py: oprot. writestructbegin ('Prepare _ cql3_query_args ')
./Cql-1.4.1/cql/Cassandra. py: classprepare_cql3_query_result:
./Cql-1.4.1/cql/Cassandra. py: oprot. writestructbegin ('Prepare _ cql3_query_result ')

M) Open cql-1.4.1/cql/Cassandra. py

Def execute_cql3_query (self, query, compression, consistency ):

"""

Parameters:

-Query

-Compression

-Consistency

"""

Print "execute_cql3_query (self, query, compression, consistency )"

Self. send_execute_cql3_query (query, compression, consistency) # send a request

Returnself. recv_execute_cql3_query () # accept the request

N) def send_execute_cql3_query (self, query, compression, consistency ):

Self. _ oprot. writemessagebegin ('execute _ cql3_query ', tmessagetype. Call, self. _ seqid)

ARGs = execute_cql3_query_args ()

Args. query = Query

Args. Compression = Compression

Args. Consistency = consistency

Args. Write (self. _ oprot)

Self. _ oprot. writemessageend ()

Self. _ oprot. Trans. Flush ()

 

The message content is as follows:

| String | 32bit | 32bit |

| Execute_cql3_query '| tmessagetype. Cal | self. _ seqid |

------------------- ARGs ----------------------------------

End

 

Send the request type to the thrift port with a fixed communication protocol.

O) grep-r 'writemessage './*

./Lib/thrift/protocol/tprotocol. py: def writemessagebegin (self, name, tType, seqid ):

./Lib/thrift/protocol/tprotocol. py: def writemessageend (Self ):

./Lib/thrift/protocol/tjsonprotocol. py: defwritemessagebegin (self, name, REQUEST_TYPE, seqid ):

./Lib/thrift/protocol/tjsonprotocol. py: def writemessageend (Self ):

./Lib/thrift/protocol/tjsonprotocol. py: defwritemessagebegin (self, name, REQUEST_TYPE, seqid ):

./Lib/thrift/protocol/tjsonprotocol. py: def writemessageend (Self ):

./Lib/thrift/protocol/tbinaryprotocol. py: def writemessagebegin (self, name, type, seqid ):

./Lib/thrift/protocol/tbinaryprotocol. py: def writemessageend (Self ):

Using the thrift interface to write port information

P) Vim./lib/thrift/protocol/tbinaryprotocol. py

Def writemessagebegin (self, name, type, seqid ):

If self. strictwrite:

Self. writei32 (tbinaryprotocol. version_1 | type)

Self. writestring (name)

Self. writei32 (seqid)

Else:

Self. writestring (name)

Self. writebyte (type)

Self. writei32 (seqid)

Q) vimlib/cql-1.4.1/cql/Cassandra. py

Def write (self, oprot ):

If oprot. _ class _ = tbinaryprotocol. tbinaryprotocolaccelerated and self. thrift_spec is not none andfastbinary is n

Ot none:

Oprot. Trans. Write (fastbinary. encode_binary (self, (self. _ class __, self. thrift_spec )))

Return

Oprot. writestructbegin ('execute _ cql3_query_args ')

If self. query is not none:

Oprot. writefieldbegin ('query', tType. String, 1)

Oprot. writestring (self. query)

Oprot. writefieldend ()

If self. compression is notnone:

Oprot. writefieldbegin ('compression', tType. i32, 2)

Oprot. writei32 (self. Compression)

Oprot. writefieldend ()

If self. Consistency is notnone:

Oprot. writefieldbegin ('consistency ', tType. i32, 3)

Oprot. writei32 (self. Consistency)

Oprot. writefieldend ()

Oprot. writefieldstop ()

Oprot. writestructend ()

Cassandra cqlsh code analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.