问:
java.sql.SQLException: Connections could not be acquired from the underlying database
I'm using Hibernate, MySQL connection is valid  but I get the following.
How do I see more details on connection string that it's connecting to?

```<code>ERROR SchemaUpdate - could not get database metadata
java.sql.SQLException: Connections could not be acquired from the underlying database!
        at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
        at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
        at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:78)
        at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
        at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:168)
        at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:356)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
        at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
        at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
        at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
        ... 10 more
ERROR SchemaUpdate - could not complete schema update
java.sql.SQLException: Connections could not be acquired from the underlying database!
        at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
        at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
        at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:78)
        at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
        at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:168)
        at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:356)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
        at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
        at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
        at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
        ... 10 more

ERROR JDBCExceptionReporter - Connections could not be acquired from the underlying database!
org.hibernate.exception.GenericJDBCException: Cannot open connection
        at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
        at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
        at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
        at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
        at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
        at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1354)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
        at com.sun.proxy.$Proxy0.beginTransaction(Unknown Source)

Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
        at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
        at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
        at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:78)
        at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
        ... 15 more
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
        at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
        at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
        at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
        ... 18 more
</code>```

here's my hibernate xml:

```<code><hibernate-configuration>
    <session-factory>

        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/test</property>

        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">xxxxxx</property>

        <property name="show_sql">false</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <property name="hibernate.c3p0.max_statements">200</property>

        <property name="hibernate.c3p0.min_size">20</property>
        <property name="hibernate.c3p0.max_size">200</property>
        <property name="hibernate.c3p0.idle_test_period">300</property>
        <property name="hibernate.c3p0.timeout">300</property>

        <property name="hibernate.c3p0.max_statements">200</property>
        <property name="current_session_context_class">thread</property>

    </session-factory>
</hibernate-configuration>
</code>```

答:
I solved this challange by following Confluence Support article.

The Causes and Resolutions as stated by the above blog post for the issue of
cannot establish a connection to its database. are

The database configuration details you entered are incorrect or have
changed. For example, the database name or database
username/password for your Confluence database. Confirm that the
database username, password, and connection URL in
/confluence.cfg.xml are incorrect, and if not
update them accordingly.

The URL for the database is incorrect.
For example, if the database connection is  jdbc:mysql://yma/confluence?autoReconnect=true, does the host 'yma'
exist and is the database server installed on 'yma'? Check
/confluence.cfg.xml to find this entry.
    The database server is not running. If your database server is down, restart it and check the logs to see why it had failed or
stopped.

The network is down (or there is a firewall in between Confluence and its database) and the connection to the database
cannot be established. If your network is down, or a new firewall is
installed, consult with your System Administrator for further
troubleshooting.

Your database password has expired.
If your password has expired (and you will only discover this if you try to connect to your
database via your database client or an external database tool such
as DbVisualizer), update your password. You may want to deactivate
expiring passwords for your Confluence database user to prevent the
problem from happening again. Be sure to consult with your DBA about
this.

Postgres specific issue: the Postgres encryption method has been altered or changed when migrating or upgrading Confluence, the
environment, etc. Check the pg_hba.conf file. If the method is set
to ident change it to md5.

MySQL specific issue: there are too many connection requests that triggered MySQL server to block Confluence from making any
requests. See this MySQL article for resolution.

Body:
I'm using Hibernate, MySQL connection is valid  but I get the following.
How do I see more details on connection string that it's connecting to?

ERROR SchemaUpdate - could not get database metadata
java.sql.SQLException: Connections could not be acquired from the underlying database!
        at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
        at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
        at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:78)
        at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
        at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:168)
        at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:356)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
        at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
        at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
        at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
        ... 10 more
ERROR SchemaUpdate - could not complete schema update
java.sql.SQLException: Connections could not be acquired from the underlying database!
        at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
        at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
        at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:78)
        at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
        at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:168)
        at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:356)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
        at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
        at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
        at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
        ... 10 more

ERROR JDBCExceptionReporter - Connections could not be acquired from the underlying database!
org.hibernate.exception.GenericJDBCException: Cannot open connection
        at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
        at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
        at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
        at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
        at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
        at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1354)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
        at com.sun.proxy.$Proxy0.beginTransaction(Unknown Source)

Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
        at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
        at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
        at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:78)
        at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
        ... 15 more
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
        at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
        at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
        at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
        ... 18 more

here's my hibernate xml:

<hibernate-configuration>
    <session-factory>

        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/test</property>

        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">xxxxxx</property>

        <property name="show_sql">false</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <property name="hibernate.c3p0.max_statements">200</property>

        <property name="hibernate.c3p0.min_size">20</property>
        <property name="hibernate.c3p0.max_size">200</property>
        <property name="hibernate.c3p0.idle_test_period">300</property>
        <property name="hibernate.c3p0.timeout">300</property>

        <property name="hibernate.c3p0.max_statements">200</property>
        <property name="current_session_context_class">thread</property>

    </session-factory>
</hibernate-configuration>


来源: stackexchange

问:
URL Rewriting based on form input
I'm creating a frontpage for my website with a single form and input text, Google-style. It's working fine, however, I want to generate a pretty URL based on the input. Let's say, my input is called "id", and using the GET method of form, and the action defined to "/go/", on submission, the URL will be:

```<code>site.com/go/?id=whateverIType
</code>```

and I want to change it to

```<code>site.com/go/whateverIType
</code>```

I was thinking on Mod Rewrite, but if the user put something in the URL, like:

```<code>site.com/go/?dontwant=this&id=whateverIType&somemore=trash
</code>```

I want to ignore the other variables but "id", and rewrite the rule.

What's the better way of get this done? Thanks in advance!

PS: I'm using CodeIgniter, maybe there's something I can use for it as well. I already have a controller for "go".

答:
I'm not familiar with CodeIgniter, but you can try the following RewriteRule

```<code>RewriteEngine on
RewriteCond %{REQUEST_URI}  ^\/go\/
RewriteCond %{QUERY_STRING} id=([^&]*)
RewriteRule (.*) /go/%1? [L,R]
</code>```

The %1 references the regex group from the previous RewriteCond, and the trailing ? will strip the querystring from the redirected URL.

Hope this helps.

ContentLicense: CC BY-SA 3.0

Title: URL Rewriting based on form input

Body:
I'm creating a frontpage for my website with a single form and input text, Google-style. It's working fine, however, I want to generate a pretty URL based on the input. Let's say, my input is called "id", and using the GET method of form, and the action defined to "/go/", on submission, the URL will be:

site.com/go/?id=whateverIType

and I want to change it to

site.com/go/whateverIType

I was thinking on Mod Rewrite, but if the user put something in the URL, like:

site.com/go/?dontwant=this&id=whateverIType&somemore=trash

I want to ignore the other variables but "id", and rewrite the rule.

What's the better way of get this done? Thanks in advance!

PS: I'm using CodeIgniter, maybe there's something I can use for it as well. I already have a controller for "go".

Body:
I'm not familiar with CodeIgniter, but you can try the following RewriteRule

RewriteEngine on
RewriteCond %{REQUEST_URI}  ^\/go\/
RewriteCond %{QUERY_STRING} id=([^&]*)
RewriteRule (.*) /go/%1? [L,R]

The %1 references the regex group from the previous RewriteCond, and the trailing ? will strip the querystring from the redirected URL.

Hope this helps.


问:
SoapClient constructor works with 7.1, fails with 7.2
My SoapClient constructor works in 7.1 but fails in 7.2 returns an error

```<code> [message:protected] => SOAP-ERROR: Parsing WSDL:
 Couldn't load from 'https://domain/url.svc?singleWsdl' : failed to load external entity
</code>```

The WSDL URI appears to be legitimate. I can call it from a browser and get a WSDL document back. Any ideas for what to look at?

    $opts = array(
            'ssl' => array(
            'ciphers' => 'RC4-SHA',
            'verify_peer' => false,
            'verify_peer_name' => false
        )
    );

    $params = array(
        'encoding' => 'UTF-8',
        'verifypeer' => false,
        'verifyhost' => false,
        'soap_version' => SOAP_1_1,
        'trace' => 1,
        'exceptions' => 1,
        'connection_timeout' => 180,
        'stream_context' => stream_context_create($opts)
    );

    try {

        $this->_client = new SoapClient($wsdl, $params);
```

<p>The appropriate libraries are enabled in PHP 7.2</p>

```[PHP_OPENSSL]
extension=php_openssl.dll
[PHP_SOAP]
extension=php_soap.dll
```

答:
In my case, the problem was caused because the certificate was not loaded.

```<code>[openssl]
openssl.cafile="/path/to/cacert.pem"
</code>```

Check if the file is exists.

ContentLicense: CC BY-SA 4.0

Body:
My SoapClient constructor works in 7.1 but fails in 7.2 returns an error

 [message:protected] => SOAP-ERROR: Parsing WSDL:
 Couldn't load from 'https://domain/url.svc?singleWsdl' : failed to load external entity

The WSDL URI appears to be legitimate. I can call it from a browser and get a WSDL document back. Any ideas for what to look at?

    $opts = array(
            'ssl' => array(
            'ciphers' => 'RC4-SHA',
            'verify_peer' => false,
            'verify_peer_name' => false
        )
    );

    $params = array(
        'encoding' => 'UTF-8',
        'verifypeer' => false,
        'verifyhost' => false,
        'soap_version' => SOAP_1_1,
        'trace' => 1,
        'exceptions' => 1,
        'connection_timeout' => 180,
        'stream_context' => stream_context_create($opts)
    );

    try {

        $this->_client = new SoapClient($wsdl, $params);

The appropriate libraries are enabled in PHP 7.2

[PHP_OPENSSL]
extension=php_openssl.dll
[PHP_SOAP]
extension=php_soap.dll

Body:
In my case, the problem was caused because the certificate was not loaded.

[openssl]
openssl.cafile="/path/to/cacert.pem"

Check if the file is exists.


问:
What is the best way to get the result through Java8 function?
I need to filter elements and then sort based on certain column. Post that I would need to find the unique entries based on combination of columns. Since it is file processing, pipe(|) is used as delimiter to denote the column value.

```<code>String s1= "12|Thor|Asgaurd|1000000|Avenger|Active"
String s2= "234|Iron man|New York|9999999|Avenger|Active"
String s3= "420|Loki|Asgaurd|||Inactive"
String s4= "12|Thor|Asgaurd Bank|1000000|Avenger HQ|Active"
</code>```

Data first needs to be filtered based on the Active/Inactive status. Then it needs to be sorted based on 4th column. Lastly, the uniqueness needs to be maintained by combining column 1,2,3.

Expected Output =

```<code>"234|Iron man|New York|9999999|Avenger|Active"
"12|Thor|Asgaurd|1000000|Avenger|Active"
</code>```

答:
It seems like you're unable to filter while everything is string only.
Try this,

create a new model class which can hold your columns.
  Ex:

```<code>class MyData{
 private String name;
 private String city;
 private String distance;
 private String organization;
 private String status;
 //And create Getter Setter method for all above fields.
}
</code>```

Now came to your main class where you can play with your code stuff.

```<code>Map<MyData> map = new HashMap<MyData>();
 MyData myData = new MyData();
 myData.setName("Thor");
 myData.setCity("Asgaurd");
 myData.setDistance("1000000");
 myData.setOrganization("Avenger");
 myData.setStatus("Active");
 map.put(12, myData);
//Same thing for all other data (note: use the loop for data insertion in map)

Map<String, MyData> sorted = map.entrySet().stream().sorted(comparingByValue()).collect(toMap(e -> e.getKey(), e -> e.getValue().getName(), (e1, e2) -> e2,LinkedHashMap::new));

System.out.println("map after sorting by values: " + sorted);
</code>```

Body:
I need to filter elements and then sort based on certain column. Post that I would need to find the unique entries based on combination of columns. Since it is file processing, pipe(|) is used as delimiter to denote the column value.

String s1= "12|Thor|Asgaurd|1000000|Avenger|Active"
String s2= "234|Iron man|New York|9999999|Avenger|Active"
String s3= "420|Loki|Asgaurd|||Inactive"
String s4= "12|Thor|Asgaurd Bank|1000000|Avenger HQ|Active"

Data first needs to be filtered based on the Active/Inactive status. Then it needs to be sorted based on 4th column. Lastly, the uniqueness needs to be maintained by combining column 1,2,3.

Expected Output =

"234|Iron man|New York|9999999|Avenger|Active"
"12|Thor|Asgaurd|1000000|Avenger|Active"

Body:
It seems like you're unable to filter while everything is string only.
Try this,

create a new model class which can hold your columns.
  Ex:

class MyData{
 private String name;
 private String city;
 private String distance;
 private String organization;
 private String status;
 //And create Getter Setter method for all above fields.
}

Now came to your main class where you can play with your code stuff.

Map<MyData> map = new HashMap<MyData>();
 MyData myData = new MyData();
 myData.setName("Thor");
 myData.setCity("Asgaurd");
 myData.setDistance("1000000");
 myData.setOrganization("Avenger");
 myData.setStatus("Active");
 map.put(12, myData);
//Same thing for all other data (note: use the loop for data insertion in map)

Map<String, MyData> sorted = map.entrySet().stream().sorted(comparingByValue()).collect(toMap(e -> e.getKey(), e -> e.getValue().getName(), (e1, e2) -> e2,LinkedHashMap::new));

System.out.println("map after sorting by values: " + sorted);


问:
equality test for atomic variables in C and gcc
I have a dummy question about atomic variables using gcc.
My machine supports __sync_add_and_fetch function; I use this call in thread A to set my_variable (int) .

I want Thread B reading that shared variable to test it against a value e.g. 20.
Is it correct to write the following

```<code>   if( __sync_bool_compare_and_swap( &my_variable, 20, 20 ) ){
          //..Ok! It is 20 so go ahead!
   }else{
          // wrong: it is not ok.
   }
</code>```

If  am not wrong in gcc the __sync_val_compare_and_swap might fail when there is a race in the shared variable, but I don't know what it does return; how does it work with __sync_bool_compare_and_swap?

Problem is what happens also when at the same time Thread A is changing the value using __sync_fetch_and_add? Is it always guaranteed that it will return the value of that sum event when __sync_bool_compare_and_swap is running concurrently?

Ideally speaking and for my purpose, I would really need a function doing exactly an atomic READ only, nor a Swap. Has C or gcc something like this?

Thanks A lot

AFG

答: Disclaimer: I haven't used the GCC _sync operations but I would imagine they are the same as the ones in MSVC. I do believe that should work. Atomic instructions are always executed as a single unit to prevent race conditions. Now, this assumes that the variable you are concerned about is not mapped to some physical hardware.</p>

Body:
I have a dummy question about atomic variables using gcc.
My machine supports __sync_add_and_fetch function; I use this call in thread A to set my_variable (int) .

I want Thread B reading that shared variable to test it against a value e.g. 20.
Is it correct to write the following

   if( __sync_bool_compare_and_swap( &my_variable, 20, 20 ) ){
          //..Ok! It is 20 so go ahead!
   }else{
          // wrong: it is not ok.
   }

If  am not wrong in gcc the __sync_val_compare_and_swap might fail when there is a race in the shared variable, but I don't know what it does return; how does it work with __sync_bool_compare_and_swap?

Problem is what happens also when at the same time Thread A is changing the value using __sync_fetch_and_add? Is it always guaranteed that it will return the value of that sum event when __sync_bool_compare_and_swap is running concurrently?

Ideally speaking and for my purpose, I would really need a function doing exactly an atomic READ only, nor a Swap. Has C or gcc something like this?

Thanks A lot

AFG


问:
Save current date
I need some help in saving the current date. Is there any way to save the current date and time in which that activity is executed?

This is my code so far to declare the current date and time:

```<code>private String currentDateTimeString =
DateFormat.getDateTimeInstance().format(new Date());
</code>```

答:
If you just want to save the most recent date and time the activity is created to the device then I would recommend using SharedPreferences. Example:

```<code>SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("DATE", currentDateTimeString);
editor.commit();
</code>```

However, if you need to persistently store the date of every time the activity is created, then you should look into using a SQLite database or a persistent storage library such as Room

Title: Save current date

Body:
I need some help in saving the current date. Is there any way to save the current date and time in which that activity is executed?

This is my code so far to declare the current date and time:

private String currentDateTimeString =
DateFormat.getDateTimeInstance().format(new Date());

Body:
If you just want to save the most recent date and time the activity is created to the device then I would recommend using SharedPreferences. Example:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("DATE", currentDateTimeString);
editor.commit();

However, if you need to persistently store the date of every time the activity is created, then you should look into using a SQLite database or a persistent storage library such as Room


问:
How to make the bot detects for a reaction on every message on a channel?
I want the bot detects always for reactions to execute a code (here I'm trying to create a channel by reacting).

I tried to use Reaction Collector but it has a timer, and it can not be used for every messages.

I need an example please, i really tried many things and i didn't find anything useful on docs.

答:
I tried to use Reaction Collector but it has a timer...

My apologies for this misconception; I was mistaken when I previously implied that collectors are restricted to a time limit.

According to the docs, omitting the time option defaults to no time limit (see CollectorOptions). Therefore, this example should continue to look for the user's reaction until the bot stops.

message.channel.send('React with .')
  .then(m => {
    const filter = (reaction, user) => user.id === message.author.id && reaction.emoji.name === '';
    const collector = m.createReactionCollector(filter, { max: 1 });

    collector.on('collect', () => {
      message.clearReactions();
      message.edit('You did it!');
    });
  })
  .catch(console.error);
```

However, as mentioned, if the bot stops, the collector will stop. There's no easy way around this. If you truly want infinite collection, the only solution I see is storing a list of messages and corresponding collectors in a database (or json file), and applying them again to existing messages in the client's ready event.

Body:
I tried to use Reaction Collector but it has a timer...

My apologies for this misconception; I was mistaken when I previously implied that collectors are restricted to a time limit.

According to the docs, omitting the time option defaults to no time limit (see CollectorOptions). Therefore, this example should continue to look for the user's reaction until the bot stops.

message.channel.send('React with .')
  .then(m => {
    const filter = (reaction, user) => user.id === message.author.id && reaction.emoji.name === '';
    const collector = m.createReactionCollector(filter, { max: 1 });

    collector.on('collect', () => {
      message.clearReactions();
      message.edit('You did it!');
    });
  })
  .catch(console.error);

However, as mentioned, if the bot stops, the collector will stop. There's no easy way around this. If you truly want infinite collection, the only solution I see is storing a list of messages and corresponding collectors in a database (or json file), and applying them again to existing messages in the client's ready event.


问:
Razor Page, How to routing with tag helper asp.net core
I don't know how to get this string ?CurrentPage=2
this is my code

```<code>@page   "/Category/{IdCategory}"
@using RN.Sieuthibamien.com.Data.Models
@model RN.Sieuthibamien.com.Pages.IndexModel
@{
    ViewData["Title"] = "Category";
    var id = Model.IdCategory;
}

</code>```

```<code>                    <div>
                        <ul class="pagination">
                            <li class="page-item @(!Model.ShowFirst ? "disabled" : "")">
                                <a asp-page="./Category/@Model.IdCategory" asp-route-CurrentPage="1" class="page-link"><i class="fa fa-fast-backward"></i></a>
                            </li>

                            <li class="page-item @(!Model.ShowPrevious ? "disabled":"")">
                                <a asp-page="./Category/@Model.IdCategory" asp-route-CurrentPage="@(Model.CurrentPage -1)" class="page-link"><i class="fa fa-step-backward"></i></a>
                            </li>
                            <li class="page-item  @(!Model.ShowNext ? "disabled":"")">
                                <a asp-page="./Category/@Model.IdCategory" asp-route-CurrentPage="@(Model.CurrentPage + 1)" class="page-link"><i class="fa fa-step-forward"></i></a>
                            </li>

                            <li class="page-item  @(!Model.ShowLast ? "disabled":"")">
                                <a asp-page="./Category/@Model.IdCategory"  asp-route-CurrentPage="@Model.TotalPages" class="page-link"><i class="fa fa-fast-forward"></i></a>
                            </li>
                        </ul>
                    </div>
</code>```

when I click on next link then route that I want is ./Category/{IdCategory}?CurrentPage=2, but not and result is ./Category/{IdCategory}, Sorry with my English, Please help me

答:
Change your tag helper to this:</p>

```<code><a asp-page="./Category" asp-route-IdCategory="@Model.IdCategory" asp-route-CurrentPage="1" class="page-link"><i class="fa fa-fast-backward"></i></a>
</code>```

Body:
I don't know how to get this string ?CurrentPage=2
this is my code

@page   "/Category/{IdCategory}"
@using RN.Sieuthibamien.com.Data.Models
@model RN.Sieuthibamien.com.Pages.IndexModel
@{
    ViewData["Title"] = "Category";
    var id = Model.IdCategory;
}

when I click on next link then route that I want is ./Category/{IdCategory}?CurrentPage=2, but not and result is ./Category/{IdCategory}, Sorry with my English, Please help me

Body:
Change your tag helper to this:

<a asp-page="./Category" asp-route-IdCategory="@Model.IdCategory" asp-route-CurrentPage="1" class="page-link"><i class="fa fa-fast-backward"></i></a>


问:
How to get a random sub-tensor from a tensor?
I want to get a random sub-tensor from a tensor, and the shape is fixed. For example,
I need to get the right tensor from the left tensor, and index is random for every row, just like this:

```<code>[[1 4 3]         [[3]     [[4]
 [3 2 1]  ----->  [2]  or  [1] (generate randomly)
 [0 3 4]]         [3]]     [0]]
</code>```

I tried tf.slice and tf.gather, It doesn't work. And I tried to write a code testcase like this：

```<code>import random
import tensorflow as tf

a = tf.convert_to_tensor([[[1, 4, 3]],
                          [[3, 2, 1]],
                          [[0, 3, 4]]])

T = a.get_shape().as_list()[0]

result_list = []

for i in range(T):
    idx = random.randint(0, 2)  # get a random idx
    result_list.append(a[i][0][idx])

y_hat = tf.reshape(tf.convert_to_tensor(result_list), shape=(T, 1))

with tf.Session() as sess:
    print(sess.run(y_hat))

    # y_hat: [[4]
    #         [1]
    #         [4]]

</code>```

In this testcase, It worked. But in a real environment, 'a'.shape=(None, 3), so

'T = a.get_shape().as_list()[0]' is not a int value, i can't iterate T by range(T).
For example:

```<code>import random
import tensorflow as tf

a = tf.placeholder(shape=(None, 3), dtype=tf.int32)

result_list = []
T = a.get_shape().as_list()[0]

for i in range(T):
    idx = random.randint(0, 2)  # get a random idx
    result_list.append(a[i][0][idx])

y_hat = tf.reshape(tf.convert_to_tensor(result_list), shape=(T, 1))

with tf.Session() as sess:

    a_instance = [[[1, 4, 3]],
                  [[3, 2, 1]],
                  [[0, 3, 4]]]

    print(sess.run(y_hat, feed_dict={a: a_instance}))

</code>```

In this case, it doesn't work. Who can tell me what should i do?

答:
I normally use the numpy library to do this.

```<code>import numpy as np

a_instance = np.array([[1,4,3],[3,2,1],[0,3,4]])
a_instance = a_instance.T # transpose the matrix
np.random.shuffle(a_instance) # it performs the shuffle of the rows
a_instance = a_instance.T
</code>```

Then you can get one column as wanted with the following code:

```<code>a_column = a_instance[:, 0]
</code>```

In this way you have the wanted random column as numpy array that you can then use with tensorflow as showed:

```<code>...
print(sess.run(y_hat, feed_dict={a: [a_column.tolist()]}))
</code>```

Remember also to use a copy of "a_instance" with the shuffle method if you do not want to modify the "a_instance" matrix permanently.

Body:
I want to get a random sub-tensor from a tensor, and the shape is fixed. For example,
I need to get the right tensor from the left tensor, and index is random for every row, just like this:

[[1 4 3]         [[3]     [[4]
 [3 2 1]  ----->  [2]  or  [1] (generate randomly)
 [0 3 4]]         [3]]     [0]]

I tried tf.slice and tf.gather, It doesn't work. And I tried to write a code testcase like this：

import random
import tensorflow as tf

a = tf.convert_to_tensor([[[1, 4, 3]],
                          [[3, 2, 1]],
                          [[0, 3, 4]]])

T = a.get_shape().as_list()[0]

result_list = []

for i in range(T):
    idx = random.randint(0, 2)  # get a random idx
    result_list.append(a[i][0][idx])

y_hat = tf.reshape(tf.convert_to_tensor(result_list), shape=(T, 1))

with tf.Session() as sess:
    print(sess.run(y_hat))

    # y_hat: [[4]
    #         [1]
    #         [4]]

In this testcase, It worked. But in a real environment, 'a'.shape=(None, 3), so

'T = a.get_shape().as_list()[0]' is not a int value, i can't iterate T by range(T).
For example:

import random
import tensorflow as tf

a = tf.placeholder(shape=(None, 3), dtype=tf.int32)

result_list = []
T = a.get_shape().as_list()[0]

for i in range(T):
    idx = random.randint(0, 2)  # get a random idx
    result_list.append(a[i][0][idx])

y_hat = tf.reshape(tf.convert_to_tensor(result_list), shape=(T, 1))

with tf.Session() as sess:

    a_instance = [[[1, 4, 3]],
                  [[3, 2, 1]],
                  [[0, 3, 4]]]

    print(sess.run(y_hat, feed_dict={a: a_instance}))

In this case, it doesn't work. Who can tell me what should i do?

Body:
I normally use the numpy library to do this.

import numpy as np

a_instance = np.array([[1,4,3],[3,2,1],[0,3,4]])
a_instance = a_instance.T # transpose the matrix
np.random.shuffle(a_instance) # it performs the shuffle of the rows
a_instance = a_instance.T

Then you can get one column as wanted with the following code:

a_column = a_instance[:, 0]

In this way you have the wanted random column as numpy array that you can then use with tensorflow as showed:

...
print(sess.run(y_hat, feed_dict={a: [a_column.tolist()]}))

Remember also to use a copy of "a_instance" with the shuffle method if you do not want to modify the "a_instance" matrix permanently.


问:
Java Optionals: Return value or do something and continue the loop
In a loop I have code similar to this

```<code>    for (Id id : ids) {
        Item item = itemRepo.findbyId(id);

        if(item == null) {
            Log.error("No item by id");
            continue;
        }

        // bla bla bla
    }
</code>```

What is the best way to rewrite this using Optionals ?

I tried to rewrite and wanted to use ifPresentOrElse to make code shorter but for the method ifPresentOrElse() is written

When we have an Optional instance, often we want to execute a specific action on the underlying value of it. On the other hand, if the Optional is empty we want to log it or track that fact by incrementing some metric.

Hence, I just can not return value if it is there.
Any suggestions?.

Thank you.

UPDATE: I'm sorry, forgot to mention that I need solution with optional as repository function getById has changed and it returns Optional as result.

答:
I am not a big fan of Optional, either. But just in case you want to use it, this might work:

List<Id> ids = new ArrayList<Id>();
for (Id id : ids) {
    Optional.ofNullable(itemRepo.findbyId(id))
        .ifPresentOrElse(i -> process(i),
            () -> Log.error("No item by id"));
}
```

<p>In my opinion, Optionals should be used togther with streams. The for-loop is an excellent candidate for a stream.</p>

<pre class="lang-java prettyprint-override"><code>ids.stream()
    .map(itemRepo::findbyId)
    .map(Optional::ofNullable)
    .forEach(o -> o.ifPresentOrElse(i -> process(i),
            () -> Log.error("No item by id")));
</code>```

Body:
In a loop I have code similar to this

    for (Id id : ids) {
        Item item = itemRepo.findbyId(id);

        if(item == null) {
            Log.error("No item by id");
            continue;
        }

        // bla bla bla
    }

What is the best way to rewrite this using Optionals ?

I tried to rewrite and wanted to use ifPresentOrElse to make code shorter but for the method ifPresentOrElse() is written

When we have an Optional instance, often we want to execute a specific action on the underlying value of it. On the other hand, if the Optional is empty we want to log it or track that fact by incrementing some metric.

Hence, I just can not return value if it is there.
Any suggestions?.

Thank you.

UPDATE: I'm sorry, forgot to mention that I need solution with optional as repository function getById has changed and it returns Optional as result.

Body:
I am not a big fan of Optional, either. But just in case you want to use it, this might work:

List<Id> ids = new ArrayList<Id>();
for (Id id : ids) {
    Optional.ofNullable(itemRepo.findbyId(id))
        .ifPresentOrElse(i -> process(i),
            () -> Log.error("No item by id"));
}

In my opinion, Optionals should be used togther with streams. The for-loop is an excellent candidate for a stream.

ids.stream()
    .map(itemRepo::findbyId)
    .map(Optional::ofNullable)
    .forEach(o -> o.ifPresentOrElse(i -> process(i),
            () -> Log.error("No item by id")));


问:
When is 23:59:60 a valid time when programming?
I was reading the article about Falsehoods programmers believe again, and came across this line:

The time 23:59:60 is always invalid

And found this rather curious... why would this be a falsehood? In what scenario would 23:59:60 be a valid time to use as a programmer?
I'm probably missing something obvious, but I'd say this should always be 00:00:00?

答:
It is called Leap second. See wiki post:
Leap second wiki article</p>


问:
need help connecting to mysql database
0

hi need to connect to a mysql server i have set up on godaddy.com

my code is

```<code>var dbcon : IDbConnection; //port is 3303
var connectionString : String = "Server=xxxxx.xx.xx.x; Uid=lxxxx; Pwd=xxxx; Database=lxxx;";

dbcon = new SqlConnection(connectionString);
dbcon.Open();
</code>```

however my connection is rejected, i suspect i need to put in a port number.can anyone tell me how would i specify a port number in my connection string?? thx

答:
Use</p>

```<code>Server = <assigned-dns-name-or-ip>:3303, Uid=<youruserid>, Pwd=<yourpassword>, Database=<databasename>
</code>```

Title: need help connecting to mysql database

Body:
0

hi need to connect to a mysql server i have set up on godaddy.com

my code is

var dbcon : IDbConnection; //port is 3303
var connectionString : String = "Server=xxxxx.xx.xx.x; Uid=lxxxx; Pwd=xxxx; Database=lxxx;";

dbcon = new SqlConnection(connectionString);
dbcon.Open();

however my connection is rejected, i suspect i need to put in a port number.can anyone tell me how would i specify a port number in my connection string?? thx

Body:
Use

Server = <assigned-dns-name-or-ip>:3303, Uid=<youruserid>, Pwd=<yourpassword>, Database=<databasename>


问:
How to fix connection issues while sending heavy files to hive table?
I have files on hdfs that I want to put in a Hive table. The operation is performed by Spark batch in a java app.
The code performing the task is the following :

[...]

final Dataset<File> fileDs = rawDs.map(record -> {
        return FileService.map(record.getList(2));
      }, Encoders.bean(File.class));

final Dataset<Row> fileDsWithId = fileDs.withColumn("id", functions.lit(id));
fileDsWithId.repartition(fileDsWithId.col("id"));

fileWithId.write().mode(SaveMode.Append)
  .format("orc")
  .partitionBy("id")
  .option("path", hdfs://..../mydatabase.db/mytable")
  .saveAsTable("mydatabase.mytable");
```

<p>When I use a small file (1 or 2 lines of data) the app is working fine, job ending successfully within 30 seconds. The table is created in Hive and I can display data using a Select * query. It's also working when the table already exists. Datas are simply added to the existing ones.
The generate table's structures in hive seems fine. It matches my datas.</p>

<p>But when I try to process a bigger file (3.7Mo with about 1000 lines of data) the job fails after 15 minutes. The corresponding orc file is created in hdfs but it's empty and Hive doesn't know about it.</p>

<p>The log file is showing several errors like these :</p>

```2019-05-31 14:20:07,500 - [ERROR] [                           dispatcher-event-loop-3] pache.spark.scheduler.cluster.YarnClusterScheduler - [{}] - Lost executor 31 on XXXXXX: Container marked as failed: container_e71_1559121287708_0019_02_000032 on host: XXXXXXXXX. Exit status: 143. Diagnostics: Container killed on request. Exit code is 143
Container exited with a non-zero exit code 143.
Killed by external signal
[...]
java.lang.RuntimeException: java.io.IOException: Connection reset by peer
        at org.spark_project.guava.base.Throwables.propagate(Throwables.java:160)
        at org.apache.spark.network.client.TransportClient.sendRpcSync(TransportClient.java:273)
        at org.apache.spark.network.crypto.AuthClientBootstrap.doSparkAuth(AuthClientBootstrap.java:105)
        at org.apache.spark.network.crypto.AuthClientBootstrap.doBootstrap(AuthClientBootstrap.java:79)
...
        at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:338)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.IOException: Connection reset by peer
        at sun.nio.ch.FileDispatcherImpl.read0(Native Method)
        at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)
        at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
        at sun.nio.ch.IOUtil.read(IOUtil.java:192)
...
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:442)
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:131)
        at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
        ... 1 more
[...]
        2019-05-31 14:20:17,898 - [ERROR] [                                shuffle-client-4-1]    org.apache.spark.network.client.TransportClient - [{}] - Failed to send RPC 9035939448873337359 to XXXXXXXX: java.nio.channels.ClosedChannelExceptionsg
java.nio.channels.ClosedChannelException
        at io.netty.channel.AbstractChannel$AbstractUnsafe.write(...)(Unknown Source)
2019-05-31 14:20:17,899 - [ERROR] [          Executor task launch worker for task 244] apache.spark.network.client.TransportClientFactory - [{}] - Exception while bootstrapping client after 5999 mssg
java.lang.RuntimeException: java.io.IOException: Failed to send RPC 9035939448873337359 to XXXXXXXXX: java.nio.channels.ClosedChannelException
        at org.spark_project.guava.base.Throwables.propagate(Throwables.java:160)
        at org.apache.spark.network.client.TransportClient.sendRpcSync(TransportClient.java:273)
        at org.apache.spark.network.sasl.SaslClientBootstrap.doBootstrap(SaslClientBootstrap.java:70)
        at org.apache.spark.network.crypto.AuthClientBootstrap.doSaslAuth(AuthClientBootstrap.java:115)
     ...
        at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:338)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.IOException: Failed to send RPC 9035939448873337359 to XXXXXXXXXXXX: java.nio.channels.ClosedChannelException
        at org.apache.spark.network.client.TransportClient.lambda$sendRpc$2(TransportClient.java:237)
        at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:507)
        at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:481)
        at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:420)
        at io.netty.util.concurrent.DefaultPromise.tryFailure(DefaultPromise.java:122)
        at io.netty.channel.AbstractChannel$AbstractUnsafe.safeSetFailure(AbstractChannel.java:852)
at io.netty.channel.AbstractChannel$AbstractUnsafe.write(AbstractChannel.java:738)
        at io.netty.channel.DefaultChannelPipeline$HeadContext.write(DefaultChannelPipeline.java:1251)
        at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:733)
        at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:725)
        at io.netty.channel.AbstractChannelHandlerContext.access$1900(AbstractChannelHandlerContext.java:35)
...
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:446)
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:131)
        at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
        ... 1 more
[...]
2019-05-31 14:20:22,907 - [INFO ] [                             Block Fetch Retry-6-1] .apache.spark.network.shuffle.RetryingBlockFetcher - [{}] - Retrying fetch (2/3) for 1 outstanding blocks after 5000 mssg
2019-05-31 14:20:27,909 - [ERROR] [                             Block Fetch Retry-6-2] .apache.spark.network.shuffle.RetryingBlockFetcher - [{}] - Exception while beginning fetch of 1 outstanding blocks (after 2 retries)sg
java.io.IOException: Failed to connect to XXXXXXXXX
        at org.apache.spark.network.client.TransportClientFactory.createClient(TransportClientFactory.java:232)
        at org.apache.spark.network.client.TransportClientFactory.createClient(TransportClientFactory.java:182)
...
        at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
        at java.lang.Thread.run(Thread.java:748)
Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connexion refused: XXXXXXXX
        at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
        at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
        at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:257)
...
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:131)
        ... 2 more
[...]
        2019-05-31 14:20:32,915 - [WARN ] [          Executor task launch worker for task 244]              org.apache.spark.storage.BlockManager - [{}] - Failed to fetch remote block broadcast_2_piece0 from BlockManagerId(1, XXXXXXX, 44787, None) (failed attempt 1)sg
org.apache.spark.SparkException: Exception thrown in awaitResult:
        at org.apache.spark.util.ThreadUtils$.awaitResult(ThreadUtils.scala:205)
        at org.apache.spark.network.BlockTransferService.fetchBlockSync(BlockTransferService.scala:105)
        at org.apache.spark.storage.BlockManager.getRemoteBytes(BlockManager.scala:642)
...
        at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:338)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.IOException: Failed to connect to XXXXXXXXX
        at org.apache.spark.network.client.TransportClientFactory.createClient(TransportClientFactory.java:232)
        at org.apache.spark.network.client.TransportClientFactory.createClient(TransportClientFactory.java:182)
...
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
        ... 1 more
Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connexion refused: XXXXXXXX
        at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
        at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
...
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:131)
        ... 2 more
```

I don't understand what's going on there. I have checked for memory issues but it seems fine. These machines are used to process way bigger files (usually dozens of gigabytes).
Why are the connection lost/denied/reset ? Is there any issue with Spark creating the table scheme beforehand that would explain this ?

UPDATED after Ram Ghadiyaram's answer :

I've tried setting spark.network.timeout to 6000s. No other timeout setting is configured in the environnement.
The result seems to be the same. The job fails after 10 minutes showing the same errors in log file :  "connection reset by peer", "fail to send RPC" and so on

Setting spark.core.connection.ack.wait.timeout, spark.storage.blockManagerSlaveTimeoutMs,  spark.shuffle.io.connectionTimeout, spark.rpc.askTimeout and spark.rpc.lookupTimeout to the same value (6000s) doesn't seem to work either.

I think my dataset is too messy to be handled properly anyway. I'll try changing the data model and then run the app again with these timeout settings.

Updated 01/07/2019 :

I have simplified the data model. The model was complex, leading to some empty structure in the dataset because the system couldn't link some fields by inheritance. I have flattened the structure so that every possible type is present as an actual attribute of the generic class, and so I removed inheritance.

To wrap it up, something like that:

File.class

  |-field1

  |-field2

  |-field3

  |- GenericClass

     |-Class1

     |-Class2

     |-Class3

Instead of an abstract class with some daughters I made a generic one with other classes as attributes.
This is quite dirty (and I don't recommand doing this), but the dataset was much cleaner this way.

I no longer had timeout issues after this change was performed. I guess the previous model was too messy to be efficiently writen by Spark.

I've tried writing in ORC and Avro format and both were okay.
In Avro I managed to write about 300000 lines within a minute, so default timeout settings are no longer a problem.

答:
Q:Why are the connection lost/denied/reset ?

```<code>org.apache.spark.SparkException: Exception thrown in awaitResult:
        at org.apache.spark.util.ThreadUtils$.awaitResult(ThreadUtils.scala:205)
</code>```

Seems like its typical timeout issue for large or heavy work load.

I dont know which version of spark you are using. but its failing here.
basically what it does is it will wait for some time (default timeout) and it will fail. see ThreadUtils

```<code>/**
   * Preferred alternative to `Await.result()`.
   *
   * This method wraps and re-throws any exceptions thrown by the underlying `Await` call, ensuring
   * that this thread's stack trace appears in logs.
   *
   * In addition, it calls `Awaitable.result` directly to avoid using `ForkJoinPool`'s
   * `BlockingContext`. Codes running in the user's thread may be in a thread of Scala ForkJoinPool.
   * As concurrent executions in ForkJoinPool may see some [[ThreadLocal]] value unexpectedly, this
   * method basically prevents ForkJoinPool from running other tasks in the current waiting thread.
   * In general, we should use this method because many places in Spark use [[ThreadLocal]] and it's
   * hard to debug when [[ThreadLocal]]s leak to other tasks.
   */
  @throws(classOf[SparkException])
  def awaitResult[T](awaitable: Awaitable[T], atMost: Duration): T = {
    try {
      // `awaitPermission` is not actually used anywhere so it's safe to pass in null here.
      // See SPARK-13747.
      val awaitPermission = null.asInstanceOf[scala.concurrent.CanAwait]
      awaitable.result(atMost)(awaitPermission)
    } catch {
      case e: SparkFatalException =>
        throw e.throwable
      // TimeoutException is thrown in the current thread, so not need to warp the exception.
      case NonFatal(t) if !t.isInstanceOf[TimeoutException] =>
        throw new SparkException("Exception thrown in awaitResult: ", t)
    }
  }
</code>```

you have to increase timeouts see networking docs

spark.network.timeout   120s    Default timeout for all network
  interactions. This config will be used in place of
  spark.core.connection.ack.wait.timeout,
  spark.storage.blockManagerSlaveTimeoutMs,
  spark.shuffle.io.connectionTimeout, spark.rpc.askTimeout or
  spark.rpc.lookupTimeout if they are not configured.

To sumup :  For small workloads timeout is enough for large work loads
  time out needs to be increasaed.

Body:
I have files on hdfs that I want to put in a Hive table. The operation is performed by Spark batch in a java app.
The code performing the task is the following :

[...]

final Dataset<File> fileDs = rawDs.map(record -> {
        return FileService.map(record.getList(2));
      }, Encoders.bean(File.class));

final Dataset<Row> fileDsWithId = fileDs.withColumn("id", functions.lit(id));
fileDsWithId.repartition(fileDsWithId.col("id"));

fileWithId.write().mode(SaveMode.Append)
  .format("orc")
  .partitionBy("id")
  .option("path", hdfs://..../mydatabase.db/mytable")
  .saveAsTable("mydatabase.mytable");

When I use a small file (1 or 2 lines of data) the app is working fine, job ending successfully within 30 seconds. The table is created in Hive and I can display data using a Select * query. It's also working when the table already exists. Datas are simply added to the existing ones.
The generate table's structures in hive seems fine. It matches my datas.

But when I try to process a bigger file (3.7Mo with about 1000 lines of data) the job fails after 15 minutes. The corresponding orc file is created in hdfs but it's empty and Hive doesn't know about it.

The log file is showing several errors like these :

2019-05-31 14:20:07,500 - [ERROR] [                           dispatcher-event-loop-3] pache.spark.scheduler.cluster.YarnClusterScheduler - [{}] - Lost executor 31 on XXXXXX: Container marked as failed: container_e71_1559121287708_0019_02_000032 on host: XXXXXXXXX. Exit status: 143. Diagnostics: Container killed on request. Exit code is 143
Container exited with a non-zero exit code 143.
Killed by external signal
[...]
java.lang.RuntimeException: java.io.IOException: Connection reset by peer
        at org.spark_project.guava.base.Throwables.propagate(Throwables.java:160)
        at org.apache.spark.network.client.TransportClient.sendRpcSync(TransportClient.java:273)
        at org.apache.spark.network.crypto.AuthClientBootstrap.doSparkAuth(AuthClientBootstrap.java:105)
        at org.apache.spark.network.crypto.AuthClientBootstrap.doBootstrap(AuthClientBootstrap.java:79)
...
        at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:338)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.IOException: Connection reset by peer
        at sun.nio.ch.FileDispatcherImpl.read0(Native Method)
        at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)
        at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
        at sun.nio.ch.IOUtil.read(IOUtil.java:192)
...
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:442)
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:131)
        at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
        ... 1 more
[...]
        2019-05-31 14:20:17,898 - [ERROR] [                                shuffle-client-4-1]    org.apache.spark.network.client.TransportClient - [{}] - Failed to send RPC 9035939448873337359 to XXXXXXXX: java.nio.channels.ClosedChannelExceptionsg
java.nio.channels.ClosedChannelException
        at io.netty.channel.AbstractChannel$AbstractUnsafe.write(...)(Unknown Source)
2019-05-31 14:20:17,899 - [ERROR] [          Executor task launch worker for task 244] apache.spark.network.client.TransportClientFactory - [{}] - Exception while bootstrapping client after 5999 mssg
java.lang.RuntimeException: java.io.IOException: Failed to send RPC 9035939448873337359 to XXXXXXXXX: java.nio.channels.ClosedChannelException
        at org.spark_project.guava.base.Throwables.propagate(Throwables.java:160)
        at org.apache.spark.network.client.TransportClient.sendRpcSync(TransportClient.java:273)
        at org.apache.spark.network.sasl.SaslClientBootstrap.doBootstrap(SaslClientBootstrap.java:70)
        at org.apache.spark.network.crypto.AuthClientBootstrap.doSaslAuth(AuthClientBootstrap.java:115)
     ...
        at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:338)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.IOException: Failed to send RPC 9035939448873337359 to XXXXXXXXXXXX: java.nio.channels.ClosedChannelException
        at org.apache.spark.network.client.TransportClient.lambda$sendRpc$2(TransportClient.java:237)
        at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:507)
        at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:481)
        at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:420)
        at io.netty.util.concurrent.DefaultPromise.tryFailure(DefaultPromise.java:122)
        at io.netty.channel.AbstractChannel$AbstractUnsafe.safeSetFailure(AbstractChannel.java:852)
at io.netty.channel.AbstractChannel$AbstractUnsafe.write(AbstractChannel.java:738)
        at io.netty.channel.DefaultChannelPipeline$HeadContext.write(DefaultChannelPipeline.java:1251)
        at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:733)
        at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:725)
        at io.netty.channel.AbstractChannelHandlerContext.access$1900(AbstractChannelHandlerContext.java:35)
...
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:446)
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:131)
        at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
        ... 1 more
[...]
2019-05-31 14:20:22,907 - [INFO ] [                             Block Fetch Retry-6-1] .apache.spark.network.shuffle.RetryingBlockFetcher - [{}] - Retrying fetch (2/3) for 1 outstanding blocks after 5000 mssg
2019-05-31 14:20:27,909 - [ERROR] [                             Block Fetch Retry-6-2] .apache.spark.network.shuffle.RetryingBlockFetcher - [{}] - Exception while beginning fetch of 1 outstanding blocks (after 2 retries)sg
java.io.IOException: Failed to connect to XXXXXXXXX
        at org.apache.spark.network.client.TransportClientFactory.createClient(TransportClientFactory.java:232)
        at org.apache.spark.network.client.TransportClientFactory.createClient(TransportClientFactory.java:182)
...
        at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
        at java.lang.Thread.run(Thread.java:748)
Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connexion refused: XXXXXXXX
        at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
        at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
        at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:257)
...
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:131)
        ... 2 more
[...]
        2019-05-31 14:20:32,915 - [WARN ] [          Executor task launch worker for task 244]              org.apache.spark.storage.BlockManager - [{}] - Failed to fetch remote block broadcast_2_piece0 from BlockManagerId(1, XXXXXXX, 44787, None) (failed attempt 1)sg
org.apache.spark.SparkException: Exception thrown in awaitResult:
        at org.apache.spark.util.ThreadUtils$.awaitResult(ThreadUtils.scala:205)
        at org.apache.spark.network.BlockTransferService.fetchBlockSync(BlockTransferService.scala:105)
        at org.apache.spark.storage.BlockManager.getRemoteBytes(BlockManager.scala:642)
...
        at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:338)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.IOException: Failed to connect to XXXXXXXXX
        at org.apache.spark.network.client.TransportClientFactory.createClient(TransportClientFactory.java:232)
        at org.apache.spark.network.client.TransportClientFactory.createClient(TransportClientFactory.java:182)
...
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
        ... 1 more
Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connexion refused: XXXXXXXX
        at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
        at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
...
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:131)
        ... 2 more

I don't understand what's going on there. I have checked for memory issues but it seems fine. These machines are used to process way bigger files (usually dozens of gigabytes).
Why are the connection lost/denied/reset ? Is there any issue with Spark creating the table scheme beforehand that would explain this ?

UPDATED after Ram Ghadiyaram's answer :

I've tried setting spark.network.timeout to 6000s. No other timeout setting is configured in the environnement.
The result seems to be the same. The job fails after 10 minutes showing the same errors in log file :  "connection reset by peer", "fail to send RPC" and so on

Setting spark.core.connection.ack.wait.timeout, spark.storage.blockManagerSlaveTimeoutMs,  spark.shuffle.io.connectionTimeout, spark.rpc.askTimeout and spark.rpc.lookupTimeout to the same value (6000s) doesn't seem to work either.

I think my dataset is too messy to be handled properly anyway. I'll try changing the data model and then run the app again with these timeout settings.

Updated 01/07/2019 :

I have simplified the data model. The model was complex, leading to some empty structure in the dataset because the system couldn't link some fields by inheritance. I have flattened the structure so that every possible type is present as an actual attribute of the generic class, and so I removed inheritance.

To wrap it up, something like that:

File.class

  |-field1

  |-field2

  |-field3

  |- GenericClass

     |-Class1

     |-Class2

     |-Class3

Instead of an abstract class with some daughters I made a generic one with other classes as attributes.
This is quite dirty (and I don't recommand doing this), but the dataset was much cleaner this way.

I no longer had timeout issues after this change was performed. I guess the previous model was too messy to be efficiently writen by Spark.

I've tried writing in ORC and Avro format and both were okay.
In Avro I managed to write about 300000 lines within a minute, so default timeout settings are no longer a problem.

Body:
Q:Why are the connection lost/denied/reset ?

org.apache.spark.SparkException: Exception thrown in awaitResult:
        at org.apache.spark.util.ThreadUtils$.awaitResult(ThreadUtils.scala:205)

Seems like its typical timeout issue for large or heavy work load.

I dont know which version of spark you are using. but its failing here.
basically what it does is it will wait for some time (default timeout) and it will fail. see ThreadUtils

/**
   * Preferred alternative to `Await.result()`.
   *
   * This method wraps and re-throws any exceptions thrown by the underlying `Await` call, ensuring
   * that this thread's stack trace appears in logs.
   *
   * In addition, it calls `Awaitable.result` directly to avoid using `ForkJoinPool`'s
   * `BlockingContext`. Codes running in the user's thread may be in a thread of Scala ForkJoinPool.
   * As concurrent executions in ForkJoinPool may see some [[ThreadLocal]] value unexpectedly, this
   * method basically prevents ForkJoinPool from running other tasks in the current waiting thread.
   * In general, we should use this method because many places in Spark use [[ThreadLocal]] and it's
   * hard to debug when [[ThreadLocal]]s leak to other tasks.
   */
  @throws(classOf[SparkException])
  def awaitResult[T](awaitable: Awaitable[T], atMost: Duration): T = {
    try {
      // `awaitPermission` is not actually used anywhere so it's safe to pass in null here.
      // See SPARK-13747.
      val awaitPermission = null.asInstanceOf[scala.concurrent.CanAwait]
      awaitable.result(atMost)(awaitPermission)
    } catch {
      case e: SparkFatalException =>
        throw e.throwable
      // TimeoutException is thrown in the current thread, so not need to warp the exception.
      case NonFatal(t) if !t.isInstanceOf[TimeoutException] =>
        throw new SparkException("Exception thrown in awaitResult: ", t)
    }
  }

you have to increase timeouts see networking docs

spark.network.timeout   120s    Default timeout for all network
  interactions. This config will be used in place of
  spark.core.connection.ack.wait.timeout,
  spark.storage.blockManagerSlaveTimeoutMs,
  spark.shuffle.io.connectionTimeout, spark.rpc.askTimeout or
  spark.rpc.lookupTimeout if they are not configured.

To sumup :  For small workloads timeout is enough for large work loads
  time out needs to be increasaed.


问:
Authenticator evaluates the request and responds by sending notifications over fidoStatus characteristic
I am making a mobile authenticator which supports CTAP BLE to communicate with WebAuthn.
I follow this document: https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-client-to-authenticator-protocol-v2.0-id-20180227.html#ble-protocol-overview

The Protocol Overview as following:

Authenticator advertises the FIDO Service.

Client scans for authenticator advertising the FIDO Service.

Client performs characteristic discovery on the authenticator.

If not already paired, the client, and authenticator SHALL perform BLE pairing and create an LTK. Authenticator SHALL only allow connections from previously bonded clients without user intervention.

Client checks if the fidoServiceRevisionBitfield characteristic is present. If so, the client selects a supported version by writing a value with a single bit set.

Client reads the fidoControlPointLength characteristic.

Client registers for notifications on the fidoStatus characteristic.

Client writes a request (e.g. an enroll request) into the fidoControlPoint characteristic.

Optionally, the client writes a CANCEL command to the fidoControlPoint characteristic to cancel the pending request.

Authenticator evaluates the request and responds by sending notifications over fidoStatus characteristic.

The protocol completes when either:

The client unregisters for notifications on the fidoStatus characteristic, or:

The connection times out and is closed by the authenticator.

At step 8, I receive the message like this: 0x83000700030000000000, I understand that

0x83 is MSG command

0x0007 is length of data

00030000000000 is data

When I receive such a message, I don't know what to response to WebAuthn as step 10 says "Authenticator evaluates the request and responds by sending notifications over fidoStatus characteristic."

答:
After a long time of checking, I found the answer. The reason is I am interacting with WebAuthn on Android Chrome which supports CTAP1/U2F. Now it's working fine when I work with Chrome (ver 75) on Desktop which support CTAP2.

The point to recognize WebAuthn supports CTAP2 is AuthenticatorGetInfo sent to authenticator. It is 0x83000104

0x83 is MSG command

0x0001 is length of content

04 is AuthenticatorGetInfo


问:
How to: Give permissions for text channel when user joins VC and revoke when user leaves VC
I'm attempting to make it so that when someone joins the Voice Channel, so the Bot will add the specific person to the text channel with the permission to read and send messages and remove the individual and their permissions when they leave the Voice Channel. I'm not overly familiar with discord.js so I'm not sure on how to do it.</p>

答:
Okay so let's take a look at the DiscordJS API:

Found here.

As we can see, there's a class called "WSEvent" (stands for WebsocketEvent). We can use this to detect when a user joins the channel...

But we can't really use that because there's no "CHANNEL_JOIN" event. So instead, we must listen for this voiceStateUpdate or VOICE_STATUS_UPDATE event.

Like so:

```<code>// voiceStateUpdate
/* Emitted whenever a user changes voice state - e.g. joins/leaves a channel, mutes/unmutes.
PARAMETER    TYPE             DESCRIPTION
oldMember    GuildMember      The member before the voice state update
newMember    GuildMember      The member after the voice state update    */
client.on("voiceStateUpdate", function(oldMember, newMember){
    console.log(`a user changes voice state`);
  // Here we can just check if newMember is in the channel that we want. Bam.
  if(newMember.voiceChannel.name == 'channelname') {
    // DO SOMETHING.
    myVoiceChannel.overwritePermissions(newMember, {
      SEND_MESSAGES: true
    });
  } else {
    myVoiceChannel.overwritePermissions(newMember, {
      SEND_MESSAGES: null
    });
  }
});
</code>```

Let's reduce this:

newMember.voiceChannel - This means what channel their connected to.

voiceChannel.name - Get the name of the channel so we can check it.

Hope I was a help, and by the way, here's a neat little cheatsheet I like to use when developing DiscordJS bots: https://gist.github.com/koad/316b265a91d933fd1b62dddfcc3ff584

Body:
Okay so let's take a look at the DiscordJS API:

Found here.

As we can see, there's a class called "WSEvent" (stands for WebsocketEvent). We can use this to detect when a user joins the channel...

But we can't really use that because there's no "CHANNEL_JOIN" event. So instead, we must listen for this voiceStateUpdate or VOICE_STATUS_UPDATE event.

Like so:

// voiceStateUpdate
/* Emitted whenever a user changes voice state - e.g. joins/leaves a channel, mutes/unmutes.
PARAMETER    TYPE             DESCRIPTION
oldMember    GuildMember      The member before the voice state update
newMember    GuildMember      The member after the voice state update    */
client.on("voiceStateUpdate", function(oldMember, newMember){
    console.log(`a user changes voice state`);
  // Here we can just check if newMember is in the channel that we want. Bam.
  if(newMember.voiceChannel.name == 'channelname') {
    // DO SOMETHING.
    myVoiceChannel.overwritePermissions(newMember, {
      SEND_MESSAGES: true
    });
  } else {
    myVoiceChannel.overwritePermissions(newMember, {
      SEND_MESSAGES: null
    });
  }
});

Let's reduce this:

newMember.voiceChannel - This means what channel their connected to.

voiceChannel.name - Get the name of the channel so we can check it.

Hope I was a help, and by the way, here's a neat little cheatsheet I like to use when developing DiscordJS bots: https://gist.github.com/koad/316b265a91d933fd1b62dddfcc3ff584


问:
How to indicate that date is null in sql
With the following code I was hoping to retrieve the records that have a null value for the ClosedDateTimeT field, but it only shows the records that have a value. Am I doing something wrong here:</p>

```<code>SELECT DISTINCT
    t.ticketid,
    t.Sub,
    i.status,
    i.OwnedByTeam,
    i.OwnedByTeamT,
    i.TypeOptions,
    i.ClosedDateTimeT,
    i.CompletedDateTime,
    i.TitleT
FROM
    ticket AS t
INNER JOIN
    Incident AS i ON t.TicketID = i.IncidentID
WHERE
    CAST(t.Sub AS DATE) >= '01-01-2019'
    AND CAST(t.Sub AS DATE ) <= '05-30-2019'
    AND i.Subcategory = 'Content Request'
    AND (i.TypeOptions = 'New/Initial Submission' OR
         i.TypeOptions = 'Am/Re')
    AND ((i.OwnedByTeamT LIKE '%C - CA%'
          AND i.TitleT LIKE '%B Build%'
          AND i.ClosedDateTimeT IS NOT NULL) OR
         (i.OwnedByTeamT LIKE '%C - CA%'
          AND i.TitleT LIKE '%B Build%'
          AND i.ClosedDateTimeT IS NULL) OR
         (i.OwnedByTeamT LIKE '%E - Cl%'
          AND i.TitleT LIKE '%OP B V%'))
</code>```

答:
Don't you mean something like this?</p>

```<code>SELECT DISTINCT t.ticketid
    ,t.Sub
    ,i.STATUS
    ,i.OwnedByTeam
    ,i.OwnedByTeamT
    ,i.TypeOptions
    ,i.ClosedDateTimeT
    ,i.CompletedDateTime
    ,i.TitleT
FROM ticket AS t
INNER JOIN Incident AS i ON t.TicketID = i.IncidentID
WHERE i.ClosedDateTimeT IS NULL
    AND CAST(t.Sub AS DATE) >= '01-01-2019'
    AND CAST(t.Sub AS DATE) <= '05-30-2019'
    AND i.Subcategory = 'Content Request'
    AND (
        i.TypeOptions = 'New/Initial Submission'
        OR i.TypeOptions = 'Am/Re'
        )
    AND (
        (
            i.OwnedByTeamT LIKE '%C - CA%'
            AND i.TitleT LIKE '%B Build%'
            )
        OR (
            i.OwnedByTeamT LIKE '%E - Cl%'
            AND i.TitleT LIKE '%OP B V%'
            )
        )
</code>```

Body:
With the following code I was hoping to retrieve the records that have a null value for the ClosedDateTimeT field, but it only shows the records that have a value. Am I doing something wrong here:

SELECT DISTINCT
    t.ticketid,
    t.Sub,
    i.status,
    i.OwnedByTeam,
    i.OwnedByTeamT,
    i.TypeOptions,
    i.ClosedDateTimeT,
    i.CompletedDateTime,
    i.TitleT
FROM
    ticket AS t
INNER JOIN
    Incident AS i ON t.TicketID = i.IncidentID
WHERE
    CAST(t.Sub AS DATE) >= '01-01-2019'
    AND CAST(t.Sub AS DATE ) <= '05-30-2019'
    AND i.Subcategory = 'Content Request'
    AND (i.TypeOptions = 'New/Initial Submission' OR
         i.TypeOptions = 'Am/Re')
    AND ((i.OwnedByTeamT LIKE '%C - CA%'
          AND i.TitleT LIKE '%B Build%'
          AND i.ClosedDateTimeT IS NOT NULL) OR
         (i.OwnedByTeamT LIKE '%C - CA%'
          AND i.TitleT LIKE '%B Build%'
          AND i.ClosedDateTimeT IS NULL) OR
         (i.OwnedByTeamT LIKE '%E - Cl%'
          AND i.TitleT LIKE '%OP B V%'))

Body:
Don't you mean something like this?

SELECT DISTINCT t.ticketid
    ,t.Sub
    ,i.STATUS
    ,i.OwnedByTeam
    ,i.OwnedByTeamT
    ,i.TypeOptions
    ,i.ClosedDateTimeT
    ,i.CompletedDateTime
    ,i.TitleT
FROM ticket AS t
INNER JOIN Incident AS i ON t.TicketID = i.IncidentID
WHERE i.ClosedDateTimeT IS NULL
    AND CAST(t.Sub AS DATE) >= '01-01-2019'
    AND CAST(t.Sub AS DATE) <= '05-30-2019'
    AND i.Subcategory = 'Content Request'
    AND (
        i.TypeOptions = 'New/Initial Submission'
        OR i.TypeOptions = 'Am/Re'
        )
    AND (
        (
            i.OwnedByTeamT LIKE '%C - CA%'
            AND i.TitleT LIKE '%B Build%'
            )
        OR (
            i.OwnedByTeamT LIKE '%E - Cl%'
            AND i.TitleT LIKE '%OP B V%'
            )
        )


问:
Should the class diagram represent a specific framework?
I know that frameworks provide useful interfaces and classes that save a lot of time in implementation phase, so my question is:

Should the framework interfaces and classes be included in my project's class
diagram design or not?

and if it is,

Does this affect the reusability of the design if I decided to change
the framework in the future?

答:
No, theoretically it shouldn't, but you're also free to do so.

As stated by the authors of UML: Rumbaugh, Jacobson, Booch
on The Unified Modeling Language Reference Manual at page 25

Across implementation languages and platforms. The UML is intended to be usable for systems implemented in various implementation languages and platforms, including programming languages, databases, 4GLs, organization documents, firmware, and so on. The front-end work should be identical or similar in all cases, while the back-end work will differ somewhat for each medium.


答: usually port number are specified as part of server name. e.g. 'localhost:8808'

Title: need help connecting to mysql database


问:
Add numbers to filename
I want to store data in different files. Therefore I want to create files as follows: data_1.log, data_2.log, ..., data_N.log. The appendix .log is not necessary but would be nice. All my approaches failed so far. Here is one sample that is probably close to what I need:

```<code>#include <stdio.h>
#include <string.h>

char get_file_name(int k){
    int i, j;
    char s1[100] = "logs/data_";
    char s2[100];
    snprintf(s2, 100, "%d", k);
    for(i = 0; s1[i] != '\0'; ++i);
    for(j = 0; s2[j] != '\0'; ++j, ++i){
        s1[i] = s2[j];
    }
    s1[i] = '\0';
    return s1;
}

int main(){

    char file_name[100];
    for(int k=0; k<10; k++){
        // Get data
        // ...
        // Create filename
        strcpy(file_name, get_file_name(k));
        printf("%s", file_name);
        // fp = fopen(file_name, "w+");
        // Write data to file
        // print_results_to_file();
        // fclose(fp);
    }

    return 0;
}
</code>```

At the moment I get the following errors which I don't understand:

```<code>    string.c: In function ‘get_file_name’:
string.c:14:12: warning: returning ‘char *’ from a function with return type ‘char’ makes integer from pointer without a cast [-Wint-conversion]
     return s1;
            ^~
string.c:14:12: warning: function returns address of local variable [-Wreturn-local-addr]
string.c: In function ‘main’:
string.c:24:27: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
         strcpy(file_name, get_file_name(k));
                           ^~~~~~~~~~~~~~~~
In file included from string.c:2:
/usr/include/string.h:121:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
 extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
              ^~~~~~
</code>```

Is there a more simpler way to create such filenames? I can't believe that there isn't one.

答:
There are several problems with your code, but the biggest one is that you are trying to return a pointer to a local variable from get_file_name.

This is a big no no since the memory allocated for char s1[100] in get_file_name is freed immediately after return.

The rest of the errors are because you forgot the * in char get_file_name(int k).

There are several possible solutions:

Pass in a char array for the function to fill.

Use a global variable (This is considered a bad practice).

Dynamically allocate the memory.

Make the local variable static (this is a bit hacky, but legal)

Title: Add numbers to filename

Body:
I want to store data in different files. Therefore I want to create files as follows: data_1.log, data_2.log, ..., data_N.log. The appendix .log is not necessary but would be nice. All my approaches failed so far. Here is one sample that is probably close to what I need:

#include <stdio.h>
#include <string.h>

char get_file_name(int k){
    int i, j;
    char s1[100] = "logs/data_";
    char s2[100];
    snprintf(s2, 100, "%d", k);
    for(i = 0; s1[i] != '\0'; ++i);
    for(j = 0; s2[j] != '\0'; ++j, ++i){
        s1[i] = s2[j];
    }
    s1[i] = '\0';
    return s1;
}

int main(){

    char file_name[100];
    for(int k=0; k<10; k++){
        // Get data
        // ...
        // Create filename
        strcpy(file_name, get_file_name(k));
        printf("%s", file_name);
        // fp = fopen(file_name, "w+");
        // Write data to file
        // print_results_to_file();
        // fclose(fp);
    }

    return 0;
}

At the moment I get the following errors which I don't understand:

    string.c: In function ‘get_file_name’:
string.c:14:12: warning: returning ‘char *’ from a function with return type ‘char’ makes integer from pointer without a cast [-Wint-conversion]
     return s1;
            ^~
string.c:14:12: warning: function returns address of local variable [-Wreturn-local-addr]
string.c: In function ‘main’:
string.c:24:27: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
         strcpy(file_name, get_file_name(k));
                           ^~~~~~~~~~~~~~~~
In file included from string.c:2:
/usr/include/string.h:121:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
 extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
              ^~~~~~

Is there a more simpler way to create such filenames? I can't believe that there isn't one.


问:
DB2 Installation in ruby
How can i connect with db2 AS400 in ruby without using jruby.
I am trying to install ibm_db gem in console , but it's throwing error like below</p>

```<code>    current directory: C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/ibm_db-.0.0/ext
    C:/Ruby25-x64/bin/ruby.exe -r ./siteconf20190531-15036-er6nbm.rb extconf.rb Detected 64-bit Ruby

    Environment variable IBM_DB_HOME is not set. Set it to your
    DB2/IBM_Data_Server_Driver installation directory and retry gem install.
</code>```

答:
The doc for the gem seems to say that only 32bit ruby is supported on Windows.

README for the IBM_DB Adapter (4.0.0) and Driver (3.0.5) (2018/05/24)
  For ActiveRecord Version >= 5.0.7 (and Rails >= 5.0.7)

Supported Operating Systems

Linux 32/64 bit

Microsoft Windows 32 bit

IBM AIX 32/64 bit

HP-UX 32/64 bit

Sun Solaris 32/64 bit

Title: DB2 Installation in ruby

Body:
How can i connect with db2 AS400 in ruby without using jruby.
I am trying to install ibm_db gem in console , but it's throwing error like below

    current directory: C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/ibm_db-.0.0/ext
    C:/Ruby25-x64/bin/ruby.exe -r ./siteconf20190531-15036-er6nbm.rb extconf.rb Detected 64-bit Ruby

    Environment variable IBM_DB_HOME is not set. Set it to your
    DB2/IBM_Data_Server_Driver installation directory and retry gem install.


问:
Relative z-index?
I've got a "Dialog" widget that pops up with a z-index of 100.  When I create another popup (a floating div), it appears underneath the dialog widget, because I haven't explicitly set the z-index on the new popup.

The structure ends up looking something like

```<code><div id="dialogbox" style="z-index: 100">
    <div>
        <div id="widgetThatCausesThePopup" />
    </div>
</div>
<div id="popupHiddenBehindTheDialog" />
</code>```

I'm using GWT to generate this HTML.  There can be arbitrary levels of nesting between dialogbox and widgetThatCausesThePopup, and the actual z-index may be arbitrary as well.

How can I ensure that the new div will be shown in front of the dialogbox?

答:
The natural CSS solution is to:

Make sure, that "dialogbox" gets a stacking context. This can be done by

setting z-index to something else than auto,

and additionally position to either relative, absolute or fixed.

Then add your popup as a child to "dialogbox". If it isn't yet, you can always move it in the DOM.

In that case, your popup doesn't need a z-index at all. This completely avoids the "z-index hell".

Example:

```<code><!doctype html>
<html>
<head>
<style type="text/css">
    #dialogbox {
        width: 400px; height: 300px;
        top: 0; left: 0;
        background-color: red;
    }
    #popup {
        width: 500px; height: 200px;
        top: 0; left: 0;
        background-color: green;
    }
</style>
</head>

<body>
<div id="dialogbox" style="z-index: 100; position: absolute;">
    <div>
        <div id="widgetThatCausesThePopup" >
            <button>Show popup</button>
        </div>
    </div>
    <div id="popup" style="position: absolute;">
        <!-- Empty divs cause really weird problems.
             Always make sure, that your divs aren't empty! -->

    </div>
</div>
</body>
</html>
</code>```

The stacking context even allows you to use z-indexes relative to the context, if you need them (note, that the child order doesn't matter, and the z-indexes don't have to be larger than 100):

```<code><div id="dialogbox" style="z-index: 100; position: absolute;">
    <div id="popup" style="position: absolute; z-index: 2;">
        <!-- Empty divs cause really weird problems.
             Always make sure, that your divs aren't empty! -->

    </div>
    <div>
        <div id="widgetThatCausesThePopup" style="position: absolute; z-index: 1;">
            <button>Show popup</button>
        </div>
    </div>
</div>
</code>```

Title: Relative z-index?

Body:
I've got a "Dialog" widget that pops up with a z-index of 100.  When I create another popup (a floating div), it appears underneath the dialog widget, because I haven't explicitly set the z-index on the new popup.

The structure ends up looking something like

I'm using GWT to generate this HTML.  There can be arbitrary levels of nesting between dialogbox and widgetThatCausesThePopup, and the actual z-index may be arbitrary as well.

How can I ensure that the new div will be shown in front of the dialogbox?

Body:
The natural CSS solution is to:

Make sure, that "dialogbox" gets a stacking context. This can be done by

setting z-index to something else than auto,

and additionally position to either relative, absolute or fixed.

Then add your popup as a child to "dialogbox". If it isn't yet, you can always move it in the DOM.

In that case, your popup doesn't need a z-index at all. This completely avoids the "z-index hell".

Example:

<!doctype html>
<html>
<head>
<style type="text/css">
    #dialogbox {
        width: 400px; height: 300px;
        top: 0; left: 0;
        background-color: red;
    }
    #popup {
        width: 500px; height: 200px;
        top: 0; left: 0;
        background-color: green;
    }
</style>
</head>

<body>
<div id="dialogbox" style="z-index: 100; position: absolute;">
    <div>
        <div id="widgetThatCausesThePopup" >
            <button>Show popup</button>
        </div>
    </div>
    <div id="popup" style="position: absolute;">
        <!-- Empty divs cause really weird problems.
             Always make sure, that your divs aren't empty! -->
        &nbsp;
    </div>
</div>
</body>
</html>

The stacking context even allows you to use z-indexes relative to the context, if you need them (note, that the child order doesn't matter, and the z-indexes don't have to be larger than 100):

<div id="dialogbox" style="z-index: 100; position: absolute;">
    <div id="popup" style="position: absolute; z-index: 2;">
        <!-- Empty divs cause really weird problems.
             Always make sure, that your divs aren't empty! -->
        &nbsp;
    </div>
    <div>
        <div id="widgetThatCausesThePopup" style="position: absolute; z-index: 1;">
            <button>Show popup</button>
        </div>
    </div>
</div>


问:
RegEx for capturing digits with an optional comma
I'm scraping this from some site and from this text " See all {a number will be there} employees".

Now this number is separated by "," as in 1,200. So what will be the regex to generalize as it could be 200 or 1,200 or 12,345,098.

Attempt

```<code>[0-9]+\.[0-9]+
</code>```

答:
This expression would simply output our desired number:

```<code>([0-9,]+)
</code>```

or

```<code>(\d+,?)
</code>```

which is a capturing group, including any digit and any optional comma. If necessary, we could bound it more if we wish to.

DEMO 1

DEMO 2

Advice:

The fourth bird:

I think repeating the comma and the digit would be more precise like
  for example \d+(?:\,\d+)* or else in a text first pattern can also
  match a separate comma and only match separate groups with the second.

RegEx Circuit

jex.im visualizes regular expressions:

Body:
I'm scraping this from some site and from this text " See all {a number will be there} employees".

Now this number is separated by "," as in 1,200. So what will be the regex to generalize as it could be 200 or 1,200 or 12,345,098.

Attempt

[0-9]+\.[0-9]+

Body:
This expression would simply output our desired number:

([0-9,]+)

or

(\d+,?)

which is a capturing group, including any digit and any optional comma. If necessary, we could bound it more if we wish to.

DEMO 1

DEMO 2

Advice:

The fourth bird:

I think repeating the comma and the digit would be more precise like
  for example \d+(?:\,\d+)* or else in a text first pattern can also
  match a separate comma and only match separate groups with the second.

RegEx Circuit

jex.im visualizes regular expressions:


问:
haskell : calculated fields in custom data types
Is it possible to do something that is equivalent to having a field in a data type that is automatically calculated from other fields in the data type. For example:

```<code>data Grid = Grid
  { x :: Int
  , y :: Int
  , c = (x * y) :: Int
  }
</code>```

and then
myGrid = Grid 5 6

or does this have to be or can only be done with Class?

```<code>data Grid = Grid
  { x :: Int
  , y :: Int
  }

class Calculated a where
  c :: a -> Int

instance Calculated Grid where
  c g = x g * y g
</code>```

答:
Without any additional requirement, that's simply a function.</p>

```<code>c :: Grid -> Int
c g = x g * y g
</code>```

Body:
Is it possible to do something that is equivalent to having a field in a data type that is automatically calculated from other fields in the data type. For example:

data Grid = Grid
  { x :: Int
  , y :: Int
  , c = (x * y) :: Int
  }

and then
myGrid = Grid 5 6

or does this have to be or can only be done with Class?

data Grid = Grid
  { x :: Int
  , y :: Int
  }

class Calculated a where
  c :: a -> Int

instance Calculated Grid where
  c g = x g * y g

Body:
Without any additional requirement, that's simply a function.

c :: Grid -> Int
c g = x g * y g


问:
in iOS how to reopen application when application is in background state?
Is it possible to reopen the app from background mode in some way?
For example, if the app in background is checking any other application is open on device it will automatically go to foreground?

Application purpose is - If user is driving and wants to open sms or any other app, my app will reopen from background

If not, can i display a message and play sound for the user to request app reopen?

答:
You can't and shouldn't. I can only imagine the unholy mess of several apps attempting to go back to the foreground.

You can do what Google Maps does and use either push or local notifications to let the user know they can go back to the app since interacting with either of the notifications will open the app back up. Note that spamming local notifications will be throttled by the iOS, and should not be abused.